これは、引用符で囲まれていないこの正規表現一致キーワードに似ていますが、javascript では、次のような正規表現があります。
/(https?:((?!&[^;]+;)[^\s:"'<)])+)/
すべての URL をタグで置き換える必要がありますが、それらが引用符内にある場合はそうではありません。どうすればこれを行うことができますか?
これは、引用符で囲まれていないこの正規表現一致キーワードに似ていますが、javascript では、次のような正規表現があります。
/(https?:((?!&[^;]+;)[^\s:"'<)])+)/
すべての URL をタグで置き換える必要がありますが、それらが引用符内にある場合はそうではありません。どうすればこれを行うことができますか?
参照されたトピックで提案されているのと同じソリューションを使用できます。
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