开发者

Javascript Regular Expression Problem

开发者 https://www.devze.com 2023-04-08 21:14 出处:网络
str = \'autocomplete=\\\\\\\"off\\\\\\\" name=\\\\\\\"composer_session_id\\\\\\\" value=\\\\\\\"1557423901\\\\\\\" \\\\\\/>\\\\u003cinput type=\\\\\\\"hidden\\\\\\\" autocomplete=\\\\\\\"off\\\\\\\
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_s开发者_开发知识库ource\":\"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".

How to get "1557423901" and "u436754_5" using JavaScript regex.match() or split or else?

Note: It's guaranteed that name will precede value in each case.


Since JavaScript doesn't have lookbehinds, I wrote this snippet that matches 'attribute=\\\"value\\\"' then removes the 'attribute=\\\" and \\\" parts.

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

Enjoy!

0

精彩评论

暂无评论...
验证码 换一张
取 消