This is similar to this regex match keywords that are not in quotes but in javascript, I have regex like this:
/(https?:((?!&[^;]+;)[^\s:"'<)])+)/
and need to replace all urls by a tags but not when they are inside quotes, How can I do this?
This is similar to this regex match keywords that are not in quotes but in javascript, I have regex like this:
/(https?:((?!&[^;]+;)[^\s:"'<)])+)/
and need to replace all urls by a tags but not when they are inside quotes, How can I do this?
You can use the same solution as proposed in the referred topic.
Code snippet in JavaScript:
var text = 'Hello this text is an <tagToReplace> example. bla bla bla "this text is inside <tagNotToReplace> a string" "random string" more text bla bla bla "foo"';
var patt1=/<[^>]*>(?=[^"]*(?:"[^"]*"[^"]*)*$)/g;
text.match(patt1);
// output: ["<tagToReplace>"]
text.replace(patt1, '<newTag>');
// output: "Hello this text is an <newTag> example. bla bla bla "this text is inside <tagNotToReplace> a string" "random string" more text bla bla bla "foo""
Explanation of the pattern is the same as proposed F.J.:
text # match the literal characters 'text' (?= # start lookahead [^"]* # match any number of non-quote characters (?: # start non-capturing group, repeated zero or more times "[^"]*" # one quoted portion of text [^"]* # any number of non-quote characters )* # end non-capturing group $ # match end of the string ) # end lookahead