Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to break a paragraph into sentences in jquery [duplicate]

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

like image 693
Ahmad Avatar asked Jul 22 '16 13:07

Ahmad


1 Answers

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)
like image 155
Emil S. Jørgensen Avatar answered Nov 15 '22 00:11

Emil S. Jørgensen