5

i want to break a paragraph into sentences in jquery. Lets say i have a paragraph

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?

i want to break it into

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?

is there any solution for that. i tried to use this function but it also separate sentence when a number came.

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

Thanks

Ahmad
  • 117
  • 8

1 Answers1

5

You can use something like /((\.|\?|\!)\s)|(\?|\!)|(\.$)/g to get the elements. Here is a pseudo breakdown of each capture group:

  1. ((\.|\?|\!)\s): any .,? or ! followed by whitespace.
  2. (\?|\!): any standalone ?or !.
  3. (\.$): any . followed by end-of-line. (this one might be unnecessary depending on the string)

Here is the rough code to get you on track:

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") + '"');

The "real deal" would properly have to replace over several rounds to account for the different symbols:

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)
Emil S. Jørgensen
  • 6,216
  • 1
  • 15
  • 28
  • 1
    Not necessary, if you want to return back correct symbol, you simply can pass function, to replace method For exampe: function(match){return match+"\n"}) which will make it look like this : console.log('"' + str.replace(/((\.|\?|\!)\s)|(\?|\!)|(\.$)/g, function(match){return match+"\n"}) + '"') or even ECMA6 arrow function : console.log('"' + str.replace(/((\.|\?|\!)\s)|(\?|\!)|(\.$)/g, m=>m+"\n") + '"') – Arturas Tamulaitis Jul 22 '16 at 13:47
  • Well that was running correctly here in the code snippet. Thanks for your answers, well i also create this expression and it solved my problem. **str.replace(/([.?!])\s*(?=[a-z]|[A-Z])/g, "$1|").split("|")** – Ahmad Jul 22 '16 at 13:54