2016-09-02 40 views
-4

我的句子,如:提取半結構化信息從一個字符串在javascript

「[巴黎:位置]和[里昂:地理位置]在法國」

我需要從它們中提取所有標記的部分(「巴黎:位置」和「里昂:位置」)。

我試過這段代碼中使用正則表達式(RegExp):

var regexEntity = new RegExp('\[.+:.+\]', 'g'); 

var text = '[Paris:location] and [Lyon:location] are in France'; 
while ((match = regexEntity.exec(text))) { 
    console.log(match); 
} 

但是,這是我的輸出得到,就好像是檢測結腸癌:

[ ':', 
    index: 6, 
    input: '[Paris:location] and [Lyon:location] are in France' ] 
[ ':', 
    index: 26, 
    input: '[Paris:location] and [Lyon:location] are in France' ] 

是我的正則表達式有什麼問題嗎?您使用其他方法獲取該信息?

+2

首先,如果你打算使用構造函數,你必須使用'VAR regexEntity =新的RegExp(「\\ +:+ \']','g');'。但是,如果您使用正則表達式文字表示法,則此問題不存在。請注意''\ [。+:。+ \]''=''[。+:。+]''(實際上匹配1個符號 - '.','+'或':')。然後,'。+'是一個貪婪的子模式,你可以使用懶惰的'+?'。然後,您可以添加捕獲組。 –

+2

這就是爲什麼我避免使用'RegExp'構造函數。每當使用RegExp構造函數構造RegEx時,請在使用前記錄正則表達式。 – Tushar

+0

我可以知道爲什麼這個問題有5個降價?我不知道它,我想避免再次重複同樣的錯誤。謝謝。 –

回答

2

.+是貪婪,你將需要使用懶惰版本的它:.+?

然後,很簡單這樣的:

var text = '[Paris:location] and [Lyon:location] are in France'; 
console.log(text.match(/\[.+?:.+?\]/g)); 
1

您可以使用非惰性搜索和正向預測的正則表達式。

var regex = /\[(.*?)(?=:location)/gi, 
 
    string = '"[Paris:location] and [Lyon:location] are in France"', 
 
    match; 
 
    
 
while ((match = regex.exec(string)) !== null) { 
 
    console.log(match[1]); 
 
}

相關問題