我有文字:
s.events="event3"
s.pageName="Forum: Index"
s.channel="forum"
s.prop1="Forum: Index"
s.prop2="Index Page"
s.prop36=""
s.prop37=""
s.prop38=""
s.prop39=""
s.prop40="53"
s.prop41="Anonymous"
s.prop42="username"
s.prop43=""
s.prop47=""
s.eVar1="Forum: Index"
s.eVar2="Index Page"
s.eVar36=""
s.eVar37=""
保存在 javascript 的 var 中,我想提取 s.prop42 引号之间的文本,给出结果:
"username"
我现在拥有的是
var regex = /\?prop.42="([^']+)"/;
var test = data.match(regex);
但似乎不起作用,有人可以帮助我吗?
最佳答案
使用这个:
var myregex = /s\.prop42="([^"]*)"/;
var matchArray = myregex.exec(yourString);
if (matchArray != null) {
thematch = matchArray[1];
}
在 the regex demo ,查看右 Pane 中的捕获组。
说明
s\.prop42="
与s.prop42="
匹配(但我们不会检索它)([^"]*)
中的括号捕获第 1 组中不是"
的任何字符:这就是我们想要的- 代码获取第 1 组捕获
关于JavaScript 正则表达式提取特定单词后面引号之间的所有字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24901063/