这类似于不在引号中但在 javascript 中的正则表达式匹配关键字,我有这样的正则表达式:
/(https?:((?!&[^;]+;)[^\s:"'<)])+)/
并且需要用标签替换所有网址,但不是当它们在引号内时,我该怎么做?
这类似于不在引号中但在 javascript 中的正则表达式匹配关键字,我有这样的正则表达式:
/(https?:((?!&[^;]+;)[^\s:"'<)])+)/
并且需要用标签替换所有网址,但不是当它们在引号内时,我该怎么做?
您可以使用与参考主题中建议的相同的解决方案。
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""
模式的解释与提议的FJ相同:
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