开发者

Js RegExp every other character

开发者 https://www.devze.com 2023-01-21 20:50 出处:网络
I have random strings that are similar to this: 2d4hk8x37m or whatever. I need to split it at every other character.

I have random strings that are similar to this:

2d4hk8x37m

or whatever. I need to split it at every other character.

To split it at every character its simply:

'2d4hk8x37m'.split('');

But i need every othe开发者_运维知识库r character so the array would be like this:

['2d', '4h', 'k8', 'x3', '7m']


var string = "2d4hk8x37m";
var matches = string.match(/.{2}/g);
console.log(matches);


There's no regex needed here. Just a simple for loop.

var hash = '2sfg43da'
var broken = [];
for(var index = 0; index < hash.length / 2; index++)
    broken.push(hash.substr(index*2, 2));
0

精彩评论

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