如何使用多个分隔符拆分字符串

时间:2022-02-17 21:35:37

I have the following string:

我有以下字符串:

var str = '15/17/*18/20/*22';

How can I split it so executing the following line:

如何拆分它以执行以下行:

var results = str.split(REGEX);

Will end up giving the following results:

最终会得到以下结果:

result = ['/','/*','/','/*'];

Thank you!

谢谢!

2 个解决方案

#1


2  

You can use split like this:

您可以像这样使用拆分:

var str = '15/17/*18/20/*22';
var m = str.split(/\d+/).filter(Boolean);
//=> ["/", "/*", "/", "/*"]

Or else (thanks to @hwnd):

或者(感谢@hwnd):

var m = str.split(/[^/*]+/).filter(Boolean);

#2


0  

code

x = '15/17/*18/20/*22'
x.split(/[0-9][0-9]*/)

result

结果

["", "/", "/*", "/", "/*", ""]

#1


2  

You can use split like this:

您可以像这样使用拆分:

var str = '15/17/*18/20/*22';
var m = str.split(/\d+/).filter(Boolean);
//=> ["/", "/*", "/", "/*"]

Or else (thanks to @hwnd):

或者(感谢@hwnd):

var m = str.split(/[^/*]+/).filter(Boolean);

#2


0  

code

x = '15/17/*18/20/*22'
x.split(/[0-9][0-9]*/)

result

结果

["", "/", "/*", "/", "/*", ""]