JavaScriptが正規表現で引用符(シングルまたはダブル)の間にないコードを取得する方法はありますか?javascriptのregexpで引用符外のコードを取得する
私はこの文字列を持っている場合:
'this is a test "this shouldn't be taken"'
結果は次のようになります。
'this is a test'
JavaScriptが正規表現で引用符(シングルまたはダブル)の間にないコードを取得する方法はありますか?javascriptのregexpで引用符外のコードを取得する
私はこの文字列を持っている場合:
'this is a test "this shouldn't be taken"'
結果は次のようになります。
'this is a test'
myString.replace(/".*?"/g, '')
はのmyStringから二重引用符の間の任意の文字列を削除します。しかし、二重引用符はエスケープされません。
、それは複数行の文字列(含む文字列で動作します\ nまたは\ r)と、それはまた、エスケープ引用符を処理する必要があります:
var removeQuotes = /(['"])(?:\\?[\s\S])*?\1/g;
var test = 'this is a test "this shouldn\'t be taken"';
test.replace(removeQuotes, ""); // 'this is a test '
test = 'this is a test "this sho\\"uldn\'t be taken"';
test.replace(removeQuotes, ""); // 'this is a test '
あなたの文字列があるため – Alsciende
結果は最後のスペースで「これはテストです」あるべき「べきではない」の引用で、有効ではありませんすべきではないですか? – Alsciende
私は、一重引用符と二重引用符の間にはないと言っていますが、解決策は一重引用符で囲まれています。 –