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

时间:2016-07-22 13:07:11

标签: 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?

我想将其分解为

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 );

由于

1 个答案:

答案 0 :(得分:5)

您可以使用/((\.|\?|\!)\s)|(\?|\!)|(\.$)/g之类的内容来获取元素。以下是每个捕获组的伪细分:

  1. ((\.|\?|\!)\s):任意.?!后跟空格。
  2. (\?|\!):任何独立的?!
  3. (\.$):任意.后跟end-of-line。 (根据字符串,这可能是不必要的)
  4. 以下是让您走上正轨的粗略代码:

    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)