开发者

How do I get the javascript split function to extract null values from a delimited string

I'm trying to parse a delimited string using the javascript split function. I'm using a double character delimiter. So for e.g. if the string contains employee related data with the following fields:

  1. Emp Id (mandatory)

  2. Name (mandatory)

  3. Age (optional)

  4. Mobile No (optional)

and the delimiter used is |* (i.e. pipe followed by star)

开发者_StackOverflow

I may have data like this

5322|*Mike|*21|*077665543

5323|*Jen|*|*077665543

5324|*Raj|*25|*

5325|*Alan|*|*

How do I extract null values into the array returned by split?

If I use Record.split(/\|\*/) It seems to ignore the null values. Do I need to use other functions like regex exec + substring to do this? The split function seems to be quite convenient except for this issue.


What you're doing is correct, and the null values are present.

>>> "5325|*Alan|*|*".split(/\|\*/)
["5325", "Alan", "", ""]


Do not confuse null with an empty string. Your regular expression splits the delimited string properly, capturing empty strings when the fields are "empty" as they should. If you need these array elements to be null, then you'd have to post-process the returned array yourself.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜