Javascript 如何匹配不在引号内的URL
这与此类似,但在javascript中,我有如下正则表达式:Javascript 如何匹配不在引号内的URL,javascript,regex,url,Javascript,Regex,Url,这与此类似,但在javascript中,我有如下正则表达式: /(https?:((?!&[^;]+;)[^\s:"'<)])+)/ /(https?:((?!&[^;]+;)[^\s:”您可以使用与所引用主题中建议的相同解决方案 JavaScript中的代码段: var text = 'Hello this text is an <tagToReplace> example. bla bla bla "this text is inside <tagNotTo
/(https?:((?!&[^;]+;)[^\s:"'<)])+)/
/(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""
-“是相同还是不同?为什么你在同一件事上问两个问题?@Antony,因为我意识到我不再需要brakets,因为在我的代码中brakets被替换的地方,我删除了另一个问题。”。
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