i need to ignore anything that is one space, and empty space at least bigger than one space should be matched...
"MARY HAD A LITTLE LAMB"
i expect
"MARY", "HAD A LITTLE", "LAMB"
The most common forms of whitespace you will use with regular expressions are the space (␣), the tab (\t), the new line (\n) and the carriage return (\r) (useful in Windows environments), and these special characters match each of their respective whitespaces.
\s is the regex character for whitespace. It matches spaces, new lines, carriage returns, and tabs.
Yes, also your regex will match if there are just spaces.
\W means "non-word characters", the inverse of \w , so it will match spaces as well.
Whitespace matching is \s
and you can supply a minimum and maximum in curly braces. You can also omit either of them, like so:
\s{2,}
So your code would be like:
"MARY HAD A LITTLE LAMB".split(/\s{2,}/)
You can test it online here!
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