如何在字符串中获取引用文本?

时间:2013-01-13 02:20:24

标签: javascript jquery regex

我有一个这样的字符串:

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

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

3 个答案:

答案 0 :(得分:3)

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

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

if(match) {
  var quoted = match[1]; // -> How are you doing?
} else {
  //no matches found
}

答案 1 :(得分:2)

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

答案 2 :(得分:1)

var examplestring = 'Person said "How are you doing?" ';
var extract = examplestring.match(/\"(.*)\"/);
alert(extract[1]);