2015-10-31 50 views
-1

在Python中,可以使用re.match(pattern,str)通過「()」獲取匹配的組。例如,如果我有:在Python中重新匹配python

str = "My name is Derek Last Name" 
re.match("My name is (.+)",str).group(1) 
//output is "Derek Last Name" 

有沒有一種方法可以在Javascript中實現相同的功能?

感謝

德里克

+1

多一點搜索可能會爲你做。 [如何訪問JavaScript正則表達式中的匹配組?](http://stackoverflow.com/questions/432493/how-do-you-access-the-matched-groups-in-a-javascript-regular-表達) – CollinD

+0

你說得對。我看到了,但我認爲這是一個不同的問題。 – derek

回答

0

您可以使用的匹配功能。

> var str = "My name is Derek Last Name" 
undefined 
> str.match(/My name is (.+)/)[1] 
'Derek Last Name'