1

对我来说,这总是一个脑筋急转弯,但我自己根本找不到...

我想用任何空白字符分割一个字符串,但以隔离任何\n出现。

例如:

输入:

"Regex expressions make your life...↵easier!"

输出:

"Regex"
"expressions"
"make"
"your"
"life..."
"↵"
"easier!"

到目前为止,我已经尝试过类似的东西,/s|[^\n] 但结果并不是很确定。

有什么线索吗?

4

2 回答 2

2

试试这个:/(\S+|\n)/g

var s = 'Regex expressions make your life...\neasier!';
s.match(/(\S+|\n)/g)
于 2012-11-30T10:48:41.240 回答
2

这有效:

[^\S\n]+

Not (NOT-Whitespace or Newline)等于Whitespace AND not newline (DeMorgan)

如果我得到你的“隔离”正确,试试这个:([^\S\n]|\n)

输入:

this is a test
regex

preg_split:

Array
(
[0] => this
[1] => is
[2] => a
[3] => test
[4] => 
[5] => regex
)

嗯,但这将与\s(not \n or \n已过时,离开^\S- 这是\s- 所以你所说的“隔离”是什么意思?

于 2012-11-30T11:21:02.767 回答