Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

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

1 Answer

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...