0
這是類似於此regex match keywords that are not in quotes但在JavaScript中,我正則表達式是這樣的:如何匹配的網址不引號內
/(https?:((?!&[^;]+;)[^\s:"'<)])+)/
,需要通過標籤來代替所有網址,但不是當他們是引號內,我怎樣才能做到這一點?
這是類似於此regex match keywords that are not in quotes但在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""
圖案的說明是一樣的提議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
[匹配的URL而不是內部brakets](HTTP:// stackoverflow.com/questions/15447284/match-urls-but-not-inside-brakets) - 它是相同還是不同?你爲什麼會問同一件事2個問題? – Antony 2013-03-16 09:21:38
@Antony,因爲我意識到我不再需要配件了,因爲在我的代碼中替換的地方,我刪除了另一個問題。 – jcubic 2013-03-16 10:15:10