简体   繁体   English

JavaScript正则表达式问题

[英]Javascript Regular Expression Problem

str = 'autocomplete=\\\"off\\\" name=\\\"composer_session_id\\\" value=\\\"1557423901\\\" \\\/>\\u003cinput type=\\\"hidden\\\" autocomplete=\\\"off\\\" name=\\\"is_explicit_place\\\" id=\\\"u436754_5\\\"';

or use this string 或使用此字符串

session_id\":1557423901,\"include_source\":\"web_composer\",\"allow_cities\":true},\"bootstrapEndpoint\":\"\\\/ajax\\\/places\\\/typeahead.php\"});},\"j4e8191ff7ff1878042874292\":function(){return new Typeahead(JSCC.get('j4e8191ff7ff1878042874291'), {node_id: \"u436754_1\",

i want that str.match() return value of composer_session_id which is "1557423901" and also the id of is_explicit_plac which is "u436754_5". 我想, str.match()的返回值composer_session_id这是“1557423901”,也是idis_explicit_plac这是“u436754_5”。

How to get "1557423901" and "u436754_5" using JavaScript regex.match() or split or else ? 如何使用JavaScript regex.match() or split or else方式获取“ 1557423901”和“ u436754_5”?

Note: It's guaranteed that name will precede value in each case. 注意:确保在每种情况下name都将优先于value

Since JavaScript doesn't have lookbehinds, I wrote this snippet that matches 'attribute=\\\\\\"value\\\\\\"' then removes the 'attribute=\\\\\\" and \\\\\\" parts. 由于JavaScript没有后顾之忧,因此我编写了与'attribute=\\\\\\"value\\\\\\"'匹配'attribute=\\\\\\"value\\\\\\"'代码段,然后删除了'attribute=\\\\\\"\\\\\\"部分。

var matches = str.match(/(?:name|id|value)=\\".*?\\"/g);
for (var key in matches)
    matches[key]=matches[key].replace(/.*?\\"(.*?)\\"/,"$1");

Enjoy! 请享用!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM