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

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?

Community
  • 1
  • 1
Okky
  • 10,338
  • 15
  • 75
  • 122
  • 2
    Look this: http://stackoverflow.com/questions/650022/how-do-i-split-a-string-with-multiple-separators-in-javascript – SamYan Oct 11 '13 at 08:26
  • Can anyone help me with this related question http://stackoverflow.com/questions/19313874/add-space-in-the-string-before-and-after-certain-characters – Okky Oct 11 '13 at 08:49

6 Answers6

64

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/

melc
  • 11,523
  • 3
  • 36
  • 41
  • 1
    This works great! what exactly is `new RegExp(separators.join('|'), 'g')` doing here though? – HussienK Mar 18 '16 at 17:05
  • @HussienK enables alternation in the expression, look here http://www.regular-expressions.info/alternation.html – melc Mar 20 '16 at 13:00
  • 2
    Man. The RegEx. This is why I've taken so long to actually learn them. Why the heck does the + have three slashes in front, but the * have 2 slashes, and the :, / and space have none? – LJD Mar 08 '19 at 17:39
12

This should work:

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

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

This way you don't need to escape anything.

anubhava
  • 761,203
  • 64
  • 569
  • 643
9

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)

samjudson
  • 56,243
  • 7
  • 59
  • 69
2

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

Friso
  • 1,080
  • 6
  • 37
1

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

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

Roy Dictus
  • 32,551
  • 8
  • 60
  • 76
  • I'm getting error when I changed it to |\ 'Uncaught SyntaxError: Unexpected token ;' I think it is because when |\ is entered inside quote the quote is getting escaped. How to avoid that? – Okky Oct 11 '13 at 08:32
1

If you want to split based in multiple regexes and dont want to write a big regex You could use replace and split. Like this:

const spliters = [
 /(\[products\])/g,
 /(\[link\])/g,
 /(\[llinks\])/g,
];
let newString = "aa [products] bb [link] [products] cc [llinks] dd";
spliters.forEach(regex => {
 newString = newString.replace(regex, match => `æææ${match}æææ`);
});
const mySplit = newString.split(/æææ([^æ]+)æææ/)
console.log(mySplit);

This works very well for my case.

Emanuel
  • 2,603
  • 22
  • 24