2011-09-27 54 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"); 

享受!