开发者

regex breaks on \n

开发者 https://www.devze.com 2023-01-01 06:08 出处:网络
segmentText = ; testRgx = [/( \\d+\\.| [^\\W\\d_]\\.|.)+?([!?.。](?= |$)|$)/g]; arrSegments = segmentText.match(testRgx);

segmentText = ;

testRgx = [/( \d+\.| [^\W\d_]\.|.)+?([!?.。](?= |$)|$)/g];


arrSegments = segmentText.match(testRgx);

This expression fails if segmentText has \n or other white spaces in it.I want to add \n in to the list of chars tha开发者_StackOverflow社区t the above pattern use [!?.。] => [!?.。\n] so that the segment is separated based on the \n character


If you add the 'm' modifier the . will match newlines

/foo/gm


In javascript there is no 's' modifier, so for instance if you want to match things inside quotes in the following text:

something foo bar "
porky" something bar foo

You could normally use /".+?"/s. Instead in JS you would use /"[\s\S]+?"/ - matching every character, as the dot will match all but newline characters.

0

精彩评论

暂无评论...
验证码 换一张
取 消