Javascript string replacement - what's the best way to do this?

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. <# something with 'quotes' #> Another 'quoted string' is found <# 

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. <# something with 'quotes' #> Another \'quoted string\' is found <# 

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 // #> and <# block with a carriage return. s = s.replace(/((^|#>)[^\t]*?)'/g, "$1\r"); // Continue replacing single quotes while we're // still finding matches. s = s.split("\r"); if (s.length < ++i) break; s = s.join("\r"); } while (true); // Replace each instance of a carriage return // with an escaped single quote. s = s.join("\\'"); 

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)?

+4
source share
2 answers

This regular expression matches quotes not inside <# ... #>

 '(?=((?!#>)[\s\S])*(<#|$)) 

brief explanation:

 ' # match a single quote (?= # start positive look ahead ( # start capture group 1 (?! # start negative look ahead # # match the character '#' > # match the character '>' ) # end negative look ahead [\s\S] # match any character from the set {'0x00'..'ΓΏ'} )* # end capture group 1 and repeat it zero or more times ( # start capture group 2 < # match the character '<' # # match the character '#' | # OR $ # match the end of the input ) # end capture group 2 ) # end positive look ahead 

or, just in English:

Match the single quote only when viewing the substring '<#' (or the end of the input) can be seen without encountering the β€œ#>” between the single quote and the β€œ<#” (or the end of the input).

But this regular expression will not be more effective than what you have (more efficient than in: faster).

Why are you looking for something other than your current approach? Your decision looks good to me.

+4
source

The following regex is very fast in the firebug console for thousands of characters.

 str.replace(/'|\\'/g, "\\'") .replace(/(<#[^#\>]*)\\'([^\\']+)\\'([^#\>]*#\>)/g, "$1'$2'$3") 

The first replaces all quotes and already escaped quotes \ The second searches for all <# ... \ '... \' ... #> and replaces it with <# ...'...'... #>

0
source

Source: https://habr.com/ru/post/1300231/


All Articles