Javascript正则表达式提取特定单词后的引号之间的所有字符 [英] Javascript regex to extract all characters between quotation marks following a specific word

查看:99
本文介绍了Javascript正则表达式提取特定单词后的引号之间的所有字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有文字:

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的引号之间提取文本,给出结果:

saved in a var in javascript and I want to extract the text between the quotes of s.prop42 giving me the result:

"username"

我现在所拥有的是

    var regex = /\?prop.42="([^']+)"/;
    var test = data.match(regex);

但它似乎没有用,有人可以帮助我吗?

but it doesnt seem to work, can someone help me out?

推荐答案

使用此:

var myregex = /s\.prop42="([^"]*)"/;
var matchArray = myregex.exec(yourString);
if (matchArray != null) {
    thematch = matchArray[1];
} 

正则表达式演示,查看右侧窗格中的捕获组

In the regex demo, look at the capture group in the right pane.

解释


  • s\.prop42 =匹配 s.prop42 =(但我们不会检索它)

  • 中的括号([^] *)捕获任何不是的字符到第1组:这就是我们想要的

  • 代码获得第1组捕获

  • s\.prop42=" matches s.prop42=" (but we won't retrieve it)
  • The parentheses in ([^"]*) capture any chars that are not a " to Group 1: this is what we want
  • The code gets the Group 1 capture

这篇关于Javascript正则表达式提取特定单词后的引号之间的所有字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆