简体   繁体   English

Javascript正则表达式:丢弃字符串匹配结束

[英]Javascript regex: discard end of string match

I want to split a string preserving the newlines. 我想分割一个保留换行符的字符串。 The string can be everything, so the code must work in any case (new lines at begin of string, at end of string, consecutive new lines...). 字符串可以是一切,所以代码必须在任何情况下都有效(字符串开头的新行,字符串的结尾,连续的新行......)。

I'm using this code: 我正在使用此代码:

var text = "abcd\nefg\n\nhijk\n"
var matches = text.match(/.*\n?/g)

which produces the following result: 产生以下结果:

[ 'abcd\n', 'efg\n', '\n', 'hijk', '' ]

That is what I need, except for the last match ( '' ). 这就是我需要的,除了最后一场比赛( '' )。

Actually I use matches.pop() in order to remove it, but I wonder if the regex could be improved in order to avoid that match. 实际上我使用matches.pop()来删除它,但我想知道是否可以改进正则表达式以避免匹配。

Bonus points if you can explain why that match is present (I can't find any reason, but I suck at regexs :-) ). 如果你可以解释为什么匹配存在(我找不到任何理由,但我在正则表达式上吮吸:-))。

Use an alternative: 使用替代方案:

var text = "abcd\nefg\n\nhijk\n";
var matches = text.match(/.+\n?|\n/g);

You can use array#filter : 你可以使用array#filter

var matches = text.match(/.*\n?/g).filter(Boolean);
//=> [ 'abcd\n', 'efg\n', '\n', 'hijk' ]

Or using a slightly different regex with non-optional \\n (but it assumes new line is always there after last line): 或者使用稍微不同的正则表达式和非可选的\\n (但它假设新行总是在最后一行之后):

var matches = text.match(/.*\n/g);
//=> [ 'abcd\n', 'efg\n', '\n', 'hijk' ]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM