0

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?

Community
  • 1
  • 1
jcubic
  • 61,973
  • 54
  • 229
  • 402
  • [Match urls but not inside brakets](http://stackoverflow.com/questions/15447284/match-urls-but-not-inside-brakets) - Is it the same or is it different? Why do you ask 2 questions on the same thing? – Antony Mar 16 '13 at 09:21
  • @Antony because I realize that I don't need brakets anymore, since in the place in my code brakets are replaced, I remove the other question. – jcubic Mar 16 '13 at 10:15

1 Answers1

1

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
Community
  • 1
  • 1
Taky
  • 5,284
  • 1
  • 20
  • 29