0

我有一个这样的字符串:

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

如何获取双引号内的字符串。具体来说,我想要一个设置为你好吗?在这种情况下。

4

3 回答 3

4

一种方法是使用正则表达式:

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 回答