如何在jquery中将段落分成句子

Ahm*_*mad 5 javascript 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?
Run Code Online (Sandbox Code Playgroud)

我想打破它

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?
Run Code Online (Sandbox Code Playgroud)

是否有任何解决方案.我试图使用这个功能,但它也在一个数字出现时分隔句子.

var result = str.match( /[^\.!\?]+[\.!\?]+/g );
Run Code Online (Sandbox Code Playgroud)

谢谢

Emi*_*sen 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") + '"');
Run Code Online (Sandbox Code Playgroud)

"实际交易"将适当地替换几轮来解释不同的符号:

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)
Run Code Online (Sandbox Code Playgroud)