2011-09-27 55 views
0
str = 'autocomplete=\\\"off\\\" name=\\\"composer_session_id\\\" value=\\\"1557423901\\\" \\\/>\\u003cinput type=\\\"hidden\\\" autocomplete=\\\"off\\\" name=\\\"is_explicit_place\\\" id=\\\"u436754_5\\\"'; 

或使用该字符串JavaScript正则表达式问题

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获取“1557423901”和“u436754_5”regex.match() or split or else

注意:在每种情况下,保证name将在value之前。

+0

是的,这是保证 – user794624

+1

为什么你需要'匹配呢?看起来'split'在这里更适合,只是分割为'id ='和'value ='。 – Anders

+0

将更容易从此字符串 – user794624

回答

1

由于JavaScript没有向后看,我写了这个片段匹配'attribute=\\\"value\\\"',然后删除'attribute=\\\"\\\"部分。

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

享受!