String str
设置为:
{"query":{"count":1,"created":"2012-02-26T21:38:27Z","lang":"en-US","results":{"title":"Google Search"}}}
我想使用正则表达式获取Google Search
。 2012-02-26T21:38:27Z
和en-US
是可变的。
这将在Javascript中发生。
感谢。
答案 0 :(得分:4)
这是一个JSON字符串,因此您不想使用正则表达式。代替:
var obj = window.JSON.parse(str);
var title = obj.query.results.title;
答案 1 :(得分:2)
我认为使用JSON parser获取所需信息会更容易。正如其他人在这里所提到的,你可以使用类似下面的内容来获得你想要的东西。
JSON.parse(str).query.results.title
如果由于某种原因你绝对必须使用RegExp,你可以尝试类似:
function parseStr(str) {
var re, matches;
re = /"results":{"title":"([^"]*)"}/;
matches = str.match(re);
if (matches && matches.length > 1) {
return matches[1];
}
}
答案 2 :(得分:1)
这是一个JSON字符串,因此解析它:
JSON.parse(str).query.results.title