Given the following test cases:
res/js/test
res\js\test
res/js\test
res\js/test
How can I split a string by either forward slash or backslash? My attempt works when the string is only backslashes(test case 1) but doesn't work for forward slashes or a mixture of both (test cases 2, 3, 4).
test.split(/[\\\/]/);
Here's my fiddled attempt
Given the following test cases:
res/js/test
res\js\test
res/js\test
res\js/test
How can I split a string by either forward slash or backslash? My attempt works when the string is only backslashes(test case 1) but doesn't work for forward slashes or a mixture of both (test cases 2, 3, 4).
test.split(/[\\\/]/);
Here's my fiddled attempt
Share Improve this question asked Nov 23, 2015 at 20:00 bflemi3bflemi3 6,79021 gold badges95 silver badges158 bronze badges2 Answers
Reset to default 10Your string does not contain any backslashes, but esaped \j
, and \t
wich is the value for tab.
Your Code is correct, but your input is not, use this:
var test = [
'res/js/test',
'res\\js\\test',
'res/js\\test',
'res\\js/test'
];
Only a escaped backslash will make a backslash in a string '\\'
This is what I ended up doing.
I replaced all backslashes with forward slashes before splitting by forward slash.
test.replace(/\\/g, '/').split('/');