C.Shoe C.Shoe - 22 days ago 8
Javascript Question

JS Regex: Double slash splitting along with other characters

I have a split statement in my JavaScript that will split spaces and semicolons, but I want to split double slashes as well. I cannot figure out how to include a double slash along with the space and semicolon.

line = lines[i].split(/[\s;]+/);


Any help is greatly appreciated.

Answer

so assuming that by "double slashes" you mean a double forward slash ( "//" ) you are going to want to do something like the following:

line = lines[i].split(/[\s;]+|\/{2}/);

Note that the matching options are being moved from between brackets, because when placed within the brackets, "{", "2", and "}" would be interpreted literally, rather than as a pattern

Comments