2013-01-08 28 views
1

我一直在嘗試使用Javascript的RegEx解析給定段落中的每個問題。但是,我越來越不想要的結果:捕獲段落中的所有問題

的Javascript

regex = /(\S.+?[.!?])(?=\s+|$)/g; 
result = regex.exec("I can see you. Where are you? I am here! How did you get there?"); 

預期結果

["Where are you?", "How did you get there?"] 

實際結果

["I can see you.", "I can see you."] 

PS:如果有更好的方法來做到這一點,我就是耳朵!

回答

2

試試這個:

var x = string.match(/\(?[A-Z][^\.!\?]+[!\.\?]\)?/g); 
x.filter(function(sentence) { 
    return sentence.indexOf('?') >= 0; 
}) 
+0

哇,我認爲這是訣竅!非常感謝! – simonwjackson

+0

不客氣,你換了答案的任何理由?在功能上相同的afaik,雖然較新的正則表達式更短一些。 – 7zark7

+0

你的過濾器結合較短的正則表達式是我發現的最好的方法。我希望我可以選擇最好:) – simonwjackson

1

JavaScript的正則表達式選項的.exec方法只返回與捕獲的第一場比賽。它還使用匹配字符串中的位置更新正則表達式對象。這就是允許你使用.exec方法遍歷一個字符串(以及爲什麼你只能獲得第一個匹配)。

嘗試使用字符串對象的.match方法代替:

regex = /(\S.+?[.!?])(?=\s+|$)/g; 
result = ("I can see you. Where are you? I am here! How did you get there?").match(regex); 

這給出了預期的結果:

[ 
    "I can see you.", 
    "Where are you?", 
    "I am here!", 
    "How did you get there?" 
] 
1
regex =/?([^.!]*)\?/g; 
text = "I can see you. Where are you? I am here! How did you get there?"; 
result = []; 
while (m = regex.exec(text)) { 
    result.push(m[1]) 
} 

輸出:

[ 'Where are you?', 
    'How did you get there?' ]