Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript RegExp for splitting text into sentences and keeping the delimiter

I am trying to use javascript's split to get the sentences out of a string but keep the delimiter eg !?.

So far I have

sentences = text.split(/[\\.!?]/); 

which works but does not include the ending punctuation for each sentence (.!?).

Does anyone know of a way to do this?

like image 222
daktau Avatar asked Aug 01 '12 14:08

daktau


People also ask

How do you split and keep a delimiter?

Summary: To split a string and keep the delimiters/separators you can use one of the following methods: Use a regex module and the split() method along with \W special character. Use a regex module and the split() method along with a negative character set [^a-zA-Z0-9] .

Can you split by regex Javascript?

To split a string by a regular expression, pass a regex as a parameter to the split() method, e.g. str. split(/[,. \s]/) . The split method takes a string or regular expression and splits the string based on the provided separator, into an array of substrings.

How do you split a sentence in Javascript?

The split() method splits a string into an array of substrings. The split() method returns the new array. The split() method does not change the original string. If (" ") is used as separator, the string is split between words.

Can split take multiple arguments Javascript?

To split a string with multiple characters, you should pass a regular expression as an argument to the split() function. You can use [] to define a set of characters, as opposed to a single character, to match.


1 Answers

You need to use match not split.

Try this.

var str = "I like turtles. Do you? Awesome! hahaha. lol!!! What's going on????"; var result = str.match( /[^\.!\?]+[\.!\?]+/g );  var expect = ["I like turtles.", " Do you?", " Awesome!", " hahaha.", " lol!!!", " What's going on????"]; console.log( result.join(" ") === expect.join(" ") ) console.log( result.length === 6); 
like image 133
Larry Battle Avatar answered Sep 16 '22 15:09

Larry Battle