Reputation: 404
It's always such a brainteaser for me but I simply can't find that out on my own...
I'd like to split a string with any white-space character BUT to isolate any \n
occurrence.
For instance:
input:
"Regex expressions make your life...↵easier!"
ouput:
"Regex"
"expressions"
"make"
"your"
"life..."
"↵"
"easier!"
So far I've tried something like /s|[^\n]
but it didn't turn out to be very conclusive.
Any clue?
Upvotes: 1
Views: 1123
Reputation: 20909
This works:
[^\S\n]+
Not (NOT-Whitespace or Newline) wich equals Whitespace AND not newline (DeMorgan)
And if i get your "isolating" right, try this: ([^\S\n]|\n)
input:
this is a test
regex
preg_split:
Array
(
[0] => this
[1] => is
[2] => a
[3] => test
[4] =>
[5] => regex
)
hmm, but that will be the same as \s
(not \n or \n
is obsolet, leaves ^\S
- which is \s
- so what you mean by "isolating"?
Upvotes: 2
Reputation: 1254
Try this: /(\S+|\n)/g
var s = 'Regex expressions make your life...\neasier!';
s.match(/(\S+|\n)/g)
Upvotes: 2