2016-07-22 82 views
4

我想在jquery中將段落分解成句子。 可以說我有一個段落如何在jQuery中將段落分解成句子

This is a wordpress plugin. its older version was 2.3.4 and new version is 2.4. But the version 2.4 had a lot of bungs. Can we solve it? 

我想將它打入

This is a wordpress plugin. 
its older version was 2.3.4 and new version is 2.4. 
But the version 2.4 had a lot of bungs. 
Can we solve it? 

是有任何解決方案。 我試圖使用這個功能,但它也單獨的句子,當一個數字來了。

var result = str.match(/[^\.!\?]+[\.!\?]+/g); 

感謝

+0

這不是simple.you可以使用庫 –

+2

[nlp_compromise](https://github.com/nlp-compromise/nlp_compromise)([示例](https://github.com/nlp-compromise/ nlp_compromise/wiki /句子解析)) – gcampbell

+0

NLP是一個很大的任務,所以它是一個大型圖書館。 – gcampbell

回答

5

您可以使用類似/((\.|\?|\!)\s)|(\?|\!)|(\.$)/g得到的元素。下面是每個捕獲組的僞故障:

  1. ((\.|\?|\!)\s):任何.?!接着空格。
  2. (\?|\!):任何獨立的?!
  3. (\.$):任何.後面跟着end-of-line。 (這個可能會根據該字符串是不必要的)

這裏是粗略的代碼,讓您的軌道上:

console.clear(); 
 
var str = 'This is a wordpress plugin. its older version was 2.3.4 and new version is 2.4. But the version 2.4 had a lot of bungs. Can we solve it?'; 
 
console.log('"' + str + '"'); 
 
console.log('Becomes:'); 
 
console.log('"' + str.replace(/((\.|\?|\!)\s)|(\?|\!)|(\.$)/g, ".\n") + '"');

「真實交易」將適當的有代替幾輪以考慮不同的符號:

console.clear(); 
 
var str = 'This is a wordpress plugin. its older version was 2.3.4 and new version is 2.4. But the version 2.4 had a lot of bungs. Can we solve it?'; 
 
str = str 
 
    //"all" 
 
    //.replace(/((\.|\?|\!)\s)|(\?|\!)|(\.$)/g,".\n") 
 
    //"." 
 
    .replace(/((\.)\s)|(\.$)/g, ".\n") 
 
    //"?" 
 
    .replace(/((\?)\s)|(\?)/g, "?\n") 
 
    //"!" 
 
    .replace(/((\!)\s)|(\!)/g, "!\n") 
 
console.log(str)

+1

沒有必要,如果你想返回正確的符號,你只需傳遞函數,以取代方法 例如:function(match){return match +「\ n 「}) 這將使它看起來像這樣:console.log('''+ str.replace(/((\。| \?| \!)\ s)|(\?| \!)|( \。$)/ g,函數(match){return match +「\ n」})+'''') 甚至ECMA6箭頭函數:console.log('''+ str.replace(/((\ | \ | | \!)\ s)|(\?| \!)|(\。$)/ g,m => m +「\ n」)+'「') –

+0

那麼,代碼片段。感謝您的回答,以及我也創建了這個表達式,它解決了我的問題。 ** str.replace(/([。?!])\ s *(?= [a-z] | [A-Z])/ g,「$ 1 |」)。split(「|」)** – Ahmad