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
You can use something like /((\.|\?|\!)\s)|(\?|\!)|(\.$)/g
to get the elements. Here is a pseudo breakdown of each capture group:
((\.|\?|\!)\s)
: any .
,?
or !
followed by whitespace.(\?|\!)
: any standalone ?
or !
.(\.$)
: 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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With