2011-12-13 29 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+)/, ""); 
} 
相关问题