Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
228 views
in Technique[技术] by (71.8m points)

javascript - Split a string to groups of 2 chars using split?

I have this simple string :

"a1a2a3"

Is there any regex expression which can be used with split command so it will split the string to a pairs ? :

["a1","a2","a3"] ?

I've tried this :

"a1a2a3".split(/(?=..)/)

But it returns ["a", "1", "a", "2", "a3"]

p.s.

I can do it with Match but im looking (if exists) for the regex expression which can help me using split.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

split for even length string:

str.split(/(?=(?:..)*$)/)

split for odd length string, the last entry has single character:

str.split(/(?=(?:..)*.$)/)

Those are basically look-aheads that check whether the number of characters ahead is odd or even. It takes advantage of the fact that the number of characters ahead at all the split positions have the same parity as the length of the string.

The pattern in the (even version) look-ahead is (?:..)*$, which checks for even number of characters (?:..)* before the end of the string $. (Note that non-capturing group (?:pattern) is used here, otherwise, capturing group will create extra entries in the split result). Similar explanation applies for the odd version.

Note that . excludes several new line characters: , , u2028 or u2029. It will produce unexpected result for string containing such characters. Replace . with [sS] (or other equivalent construct) to make it works for all cases.


For practical purpose, match is the right tool for the job:

str.match(/..?/g)

For example:

"1234567890".match(/..?/g)
> [ "12", "34", "56", "78", "90" ]

"1234567890".match(/..?/g)
> [ "12", "34", "56", "78", "9" ]

The solution can be extended to group of n characters:

str.match(/.{1,<n>}/g)

For example:

"123456789012345678901234567890".match(/.{1,7}/g)
> [ "1234567", "8901234", "5678901", "2345678", "90" ]

It simply takes advantage of the greedy quantifier and creates groups of n characters, before running out of characters to match for the last group.

Same as above, you may want to change . to [sS] to make it work for all cases.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...