Javascript正则表达式问题

Javascript Regular Expression Problem

本文关键字:问题 正则表达式 Javascript      更新时间:2023-09-26
str = 'autocomplete='''"off'''" name='''"composer_session_id'''" value='''"1557423901'''" '''/>''u003cinput type='''"hidden'''" autocomplete='''"off'''" name='''"is_explicit_place'''" id='''"u436754_5'''"';

或者使用这个字符串

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'",

我希望composer_session_idstr.match()返回值为"1557423901",is_explicit_placid返回值为"u436754_5"。

如何使用JavaScript regex.match() or split or else获取"1557423901"answers"u436754_5" ?

注意:保证name在每种情况下都在value之前

由于JavaScript没有回溯,所以我编写了这个匹配'attribute='''"value'''"'的代码片段,然后删除了'attribute='''"'''"部分。

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

享受吧!