这与此regex match keywords that are not in quotes类似,但在javascript中,我有这样的正则表达式:
/(https?:((?!&[^;]+;)[^\s:"'<)])+)/
并且需要用标签替换所有网址,但是当它们在引号内时不能替换,我该怎么做?
答案 0 :(得分:1)
您可以使用与推荐主题中提议的解决方案相同的解决方案。
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""
模式说明与提议的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