I had a problem converting a given input string to a given output string using regular expressions in Javascript. I'm not even sure that what I'm trying to accomplish can be done using regular expressions or will be most effective using other means. I hope someone can help:
I have the following input line:
#> Some text goes here, and a 'quoted string' is inside. <
I need to replace each quote character with an ' escaped version \' whenever it is between the sequence #> and <# .
Desired output line:
#> Some text goes here, and a \'quoted string\' is inside. <
Note that the quotation marks in <# something with 'quotes' #> were not escaped, but only the quotation marks found between #> and <# .
I use the following code to accomplish this , but I would like to find a better or more efficient way to do the same (NOTE: carriage returns and guaranteed tabs cannot be found in my input line, so I can use them as follows:
var s = ... some input string ...; // Replace all "<#" sequences with tabs "\t" s = s.split("<#").join("\t"); var i = 1; do { // Replace a single quote that is found within //
The main reason I don't use just one regex is because I can't get it to replace more than one single quote character. So I resorted to a do / while loop to ensure that all of them were escaped.
Does anyone have a better way (please)?
source share