2016-07-31 54 views
1

我想提取一切從下面的字符串,除了「從」和非捕獲組「從」捕獲單詞排除重複的話

源自古法語,由拉丁語innocentia,來自無辜 - '不傷害'(基於nocere'傷害')。

這是我的正則表達式:

(?:from)(.*)(?:,.from)(.*) 

對於這個表達式,我會得到Old French, from Latin innocentiainnocent- ‘not harming’ (based on nocere ‘injure’).結果。如何編輯我的正則表達式片段,以便它符合預期的條件而不重複非捕獲組(?:,.from)

結果應該是:

  • Old French
  • Latin innocentia
  • innocent- ‘not harming’ (based on nocere ‘injure’).
+0

你的語言是什麼? –

+0

我正在使用節點JS。 –

回答

1
line="from Old French, from Latin innocentia, from innocent- ‘not harming’ (based on nocere ‘injure’)." 
line.split(/, from|from/) 

=>

[ '', 
' Old French', 
' Latin innocentia', 
' innocent- ‘not harming’ (based on nocere ‘injure’).' ] 

這可能會足夠接近。 在線試試:https://repl.it/Chp8

+0

非常感謝! –

0

您可以使用正則表達式來分割字符串。這將返回相同的結果,比使用回溯噩夢.*更好的速度。

你可以使用這個表達式(基於關你的)這樣做的:在分裂

(,.)?from 

更多信息,可以發現here

+0

謝謝。你的回答也幫助了我。除了'split'功能。有沒有辦法解決這個使用純正則表達式? –

+0

@DuyNguyen與正常的JS正則表達式重複捕獲組不同。請參閱:http://stackoverflow.com/a/3537914/6083675 – Laurel