使用regex通过匹配的数字值分割数字字符串

时间:2022-09-13 13:26:31

How can I split a string containing only numbers via matching digit values using a regex.

如何使用正则表达式将只包含数字的字符串分割为数字。

For example:

例如:

"11222344"

would return

将返回

["11","222","3","44"]

1 个解决方案

#1


2  

Use back-reference to search for same characters next to each other.

使用反向引用搜索相邻的相同字符。

https://www.regular-expressions.info/backref.html

https://www.regular-expressions.info/backref.html

You can do it like this:

你可以这样做:

var res = '11222344'.match(/(\d)\1*/g);

console.log(res);

#1


2  

Use back-reference to search for same characters next to each other.

使用反向引用搜索相邻的相同字符。

https://www.regular-expressions.info/backref.html

https://www.regular-expressions.info/backref.html

You can do it like this:

你可以这样做:

var res = '11222344'.match(/(\d)\1*/g);

console.log(res);