简体   繁体   English

为什么使用 String.split(regex) 在结果数组的开头包含一个空字符串?

[英]Why does using a String.split(regex) include an empty string at the beginning of resulting array?

I have a string data that is a numbered list (eg "1.Line 1\n2. Line 2\nLine 2 Continued\n3. Line 3" ) I tried making a regex that splits the string into ["Line 1\n", "Line 2\nLine 2 Continued\n", "Line 3"] and I made this regex const reg = /\d+\./gm .我有一个字符串data ,它是一个编号列表(例如"1.Line 1\n2. Line 2\nLine 2 Continued\n3. Line 3" )我尝试制作一个将字符串拆分为["Line 1\n", "Line 2\nLine 2 Continued\n", "Line 3"]的正则表达式["Line 1\n", "Line 2\nLine 2 Continued\n", "Line 3"]我做了这个正则表达式const reg = /\d+\./gm When I use the following regex via data.split(reg) , I do see the elements in the desired output, but I also see a leading "" element in the beginning of the array - ["","Line 1\n", "Line 2\nLine 2 Continued\n", "Line 3"] .当我通过data.split(reg)使用以下正则表达式时,我确实看到了所需 output 中的元素,但我还在数组的开头看到了一个前导""元素 - ["","Line 1\n", "Line 2\nLine 2 Continued\n", "Line 3"] . Any explanation as to why this "" exists?关于为什么这个""存在的任何解释? Any way to prevent this from showing besides just slicing the array, or even the proper regex if this one is wrong.除了对数组进行切片之外,任何防止这种情况出现的方法,或者如果这个是错误的,甚至是正确的正则表达式。

Your initial empty string exists because your split applies also to the first number 1 "1.Line 1\n , which will separate everything after your regex ( Like 1\n ) from anything before ( "" ).您的初始空字符串存在,因为您的拆分也适用于第一个数字 1 "1.Line 1\n ,它将您的正则表达式( Like 1\n )之后的所有内容与之前的任何内容( "" )分开。

In order to fix this problem, you can try making your regex more specific, so that it will apply the split beginning from the second line.为了解决这个问题,您可以尝试使您的正则表达式更具体,以便它将应用从第二行开始的拆分。 One way of doing this is adding up the \n to the regex (which can be found only from the second line on as needed).这样做的一种方法是将\n添加到正则表达式(只能根据需要从第二行中找到)。

const reg = \\n/\d+\./gm

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

相关问题 为什么带有包含捕获组的正则表达式的string.split返回一个以空字符串结尾的数组? - Why does string.split with a regular expression that contains a capturing group return an array that ends with an empty string? 为什么在分隔符与字符串值相同的长度为 1 的字符串上使用 string.split() 返回长度为 2 的数组 - Why does using string.split() on a string of length one where the delimiter is the same as the string value return an array of length two 如何在正则表达式 String.split() 中包含分隔符? - How can I include the delimiter with regex String.split()? 使用String.split返回的数组 - Using array returned by String.split 为什么string.split()结果包含undefined? - Why do string.split() results include undefined? 为什么string.split()对于正则表达式的行为有所不同? - Why does string.split() behave differently for Regular Expressions? 为什么JavaScript string.split(/ [-_] + /)的作用好像是[group]中包含引号('和“)一样? - Why does JavaScript string.split(/[ -_]+/) act as if quotes (' and ") are included in the [group]? 为什么我的string.split()调用在Internet Explorer中失败? - Why does my string.split() call fail in internet explorer? 用值分割数组-像String.split - split Array with value - like String.split Javascript - string.split(正则表达式)保留分隔符 - Javascript - string.split(regex) keep separators
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM