次のような文字列があります。
var examplestring = 'Person said "How are you doing?" ';
二重引用符内の文字列を取得するにはどうすればよいですか。具体的には、How are you doing? に設定された var が必要です。この場合。
次のような文字列があります。
var examplestring = 'Person said "How are you doing?" ';
二重引用符内の文字列を取得するにはどうすればよいですか。具体的には、How are you doing? に設定された var が必要です。この場合。
1 つの方法は、正規表現を使用することです。
var match = exampleString.match(/"([^"]*)"/);
if(match) {
var quoted = match[1]; // -> How are you doing?
} else {
//no matches found
}
var quotedString = examplestring.split('"')[1];
これは、各 " で次のように分割されます。
quotedString[0] = "Person said ";
quotedString[1] = "How are you doing?"
quotedString[2] = " ";
次に、新しい配列のインデックス 1 から選択し、「お元気ですか?」を返します。(引用符なし)。
https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/String/split
var examplestring = 'Person said "How are you doing?" ';
var extract = examplestring.match(/\"(.*)\"/);
alert(extract[1]);