0

次のような文字列があります。

var examplestring = 'Person said "How are you doing?" ';

二重引用符内の文字列を取得するにはどうすればよいですか。具体的には、How are you doing? に設定された var が必要です。この場合。

4

3 に答える 3

4

1 つの方法は、正規表現を使用することです。

var match = exampleString.match(/"([^"]*)"/);

if(match) {
  var quoted = match[1]; // -> How are you doing?
} else {
  //no matches found
}
于 2013-01-13T02:27:13.547 に答える
3
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

于 2013-01-13T02:23:04.030 に答える
1
var examplestring = 'Person said "How are you doing?" ';
var extract = examplestring.match(/\"(.*)\"/);
alert(extract[1]);
于 2013-01-13T02:25:16.180 に答える