The following code results in undefined element at the middle
"Hello World\n\nhello world".split(/\n(\n|\t|\s)*?\n/)
"Hello World\n\nhello world".split(/\n(\n|\t|\s)*\n/)
The output is
["Hello World", undefined, "hello world"]
I wanted to split if there were two new line characters with any number of new line or space or tab character between them as long as they are not alphabets or symbols or numbers.
You do not only have to use literal strings for splitting strings into an array with the split method. You can use regex as breakpoints that match more characters for splitting a string.
Split(Char[]) method, except that Regex. Split splits the string at a delimiter determined by a regular expression instead of a set of characters. The string is split as many times as possible. If no delimiter is found, the return value contains one element whose value is the original input string.
split is faster, but complex separators which might involve look ahead, Regex is only option.
split(String regex) method splits this string around matches of the given regular expression. This method works in the same way as invoking the method i.e split(String regex, int limit) with the given expression and a limit argument of zero. Therefore, trailing empty strings are not included in the resulting array.
It's because when you use a capture group in a split pattern, javascript includes the content of the capture group in the result. Since, the capture group can't be empty, it is never repeated, that's why you get "undefined" and not an empty string.
To prevent this, use a non-capturing group or a character class:
"Hello World\n\nhello world".split(/\n(?:\n|\t|\s)*\n/)
"Hello World\n\nhello world".split(/\n\s*\n/) # (\t and \n are already included in \s)
Note: if you want to remove leading and trailing spaces too, you can use:
/(?:[^\S\n]*\n){2}\s*/
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