2011-12-13 35 views
-2

如何使用正則表達式提取「text:」之後的單詞?輸入是這樣的:如何使用javascript查找匹配文本

some string some string (text:the_word_i_want_to_get some string) some string 
some string some string (text:next_word_i_want_to_get some string) some string 

和輸出應該是:

["the_word_i_want_to_get", "next_word_i_want_to_get"] 
+0

你能解釋一下這個好一點嗎? – Purag

+0

@David:我試着更清楚地重寫你的問題;新版本還在說你想要什麼? –

回答

0

可以匹配第一出現的詞是這樣的:

var input = "some string...",// Assume this is what you wrote in your question. 
    match = input.match(/text:(\w+)/)[1]; 

要獲得所有的比賽,你可以把它放在一個循環中:

while (/text:(\w+)/) { 
    matches.push(input.match(/text:(\w+)/)[1]); 
    input = input.replace(/text:(\w+)/, ""); 
} 
相關問題