基于多个分隔符分割字符串

时间:2022-08-22 13:12:41

I was trying to split a string based on multiple delimiters by referring How split a string in jquery with multiple strings as separator

我试图基于多个分隔符分割一个字符串,方法是引用jquery如何使用多个字符串作为分隔符分割一个字符串

Since multiple delimiters I decided to follow

由于我决定遵循多个分隔符

var separators = [' ', '+', '-', '(', ')', '*', '/', ':', '?'];
var tokens = x.split(new RegExp(separators.join('|'), 'g'));​​​​​​​​​​​​​​​​​

But I'm getting error

但我得到错误

Uncaught SyntaxError: Invalid regular expression: / |+|-|(|)|*|/|:|?/: Nothing to repeat 

How to solve it?

如何解决这个问题?

5 个解决方案

#1


45  

escape needed for regex related characters +,-,(,),*,?

正则表达式相关字符+、-、()、*需要的转义?

var x = "adfds+fsdf-sdf";

var separators = [' ', '\\\+', '-', '\\\(', '\\\)', '\\*', '/', ':', '\\\?'];
console.log(separators.join('|'));
var tokens = x.split(new RegExp(separators.join('|'), 'g'));
console.log(tokens);

http://jsfiddle.net/cpdjZ/

http://jsfiddle.net/cpdjZ/

#2


6  

This should work:

这应该工作:

var separators = [' ', '+', '(', ')', '*', '\\/', ':', '?', '-'];
var tokens = x.split(new RegExp('[' + separators.join('') + ']', 'g'));​​​​​​​​​​​​​​​​​

Generated regex will be using regex character class: /[ +()*\/:?-]/g

生成的regex将使用regex字符类:/[+()*\/:-]/g。

This way you don't need to escape anything.

这样你就不需要逃避任何事情。

#3


4  

The following would be an easier way of accomplishing the same thing.

下面是一种更简单的方法来完成同样的事情。

var tokens = x.split(new RegExp('[-+()*/:? ]', 'g'));​​​​​​​​​​​​​​​​​

Note that - must come first (or be escaped), otherwise it will think it is the range operator (e.g. a-z)

注意-必须先来(或被转义),否则它会认为它是range操作符(例如a-z)

#4


2  

I think you would need to escape the +, * and ?, since they've got special meaning in most regex languages

我认为您需要转义+、*和?,因为它们在大多数regex语言中都有特殊的含义

#5


1  

This is because characters like + and * have special meaning in Regex.

这是因为像+和*这样的字符在Regex中有特殊的含义。

Change your join from | to |\ and you should be fine, escaping the literals.

将您的连接从|更改为|\,您应该没有问题,避免使用文字。

#1


45  

escape needed for regex related characters +,-,(,),*,?

正则表达式相关字符+、-、()、*需要的转义?

var x = "adfds+fsdf-sdf";

var separators = [' ', '\\\+', '-', '\\\(', '\\\)', '\\*', '/', ':', '\\\?'];
console.log(separators.join('|'));
var tokens = x.split(new RegExp(separators.join('|'), 'g'));
console.log(tokens);

http://jsfiddle.net/cpdjZ/

http://jsfiddle.net/cpdjZ/

#2


6  

This should work:

这应该工作:

var separators = [' ', '+', '(', ')', '*', '\\/', ':', '?', '-'];
var tokens = x.split(new RegExp('[' + separators.join('') + ']', 'g'));​​​​​​​​​​​​​​​​​

Generated regex will be using regex character class: /[ +()*\/:?-]/g

生成的regex将使用regex字符类:/[+()*\/:-]/g。

This way you don't need to escape anything.

这样你就不需要逃避任何事情。

#3


4  

The following would be an easier way of accomplishing the same thing.

下面是一种更简单的方法来完成同样的事情。

var tokens = x.split(new RegExp('[-+()*/:? ]', 'g'));​​​​​​​​​​​​​​​​​

Note that - must come first (or be escaped), otherwise it will think it is the range operator (e.g. a-z)

注意-必须先来(或被转义),否则它会认为它是range操作符(例如a-z)

#4


2  

I think you would need to escape the +, * and ?, since they've got special meaning in most regex languages

我认为您需要转义+、*和?,因为它们在大多数regex语言中都有特殊的含义

#5


1  

This is because characters like + and * have special meaning in Regex.

这是因为像+和*这样的字符在Regex中有特殊的含义。

Change your join from | to |\ and you should be fine, escaping the literals.

将您的连接从|更改为|\,您应该没有问题,避免使用文字。