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?
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] .
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.
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.
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.
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);
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