Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Split string with backslash

I need to split a string with a backslash.

I M \ SPLITTING

My expected result :

[ "I M ", " SPLITTING" ]

I have tryied the following :

console.log(("I M \ SPLITING").split("\"));

console.log(("I M \ SPLITING").split("\\"));

Works fine with slash :

console.log(("I M / SPLITING").split("/"));
like image 849
flofreelance Avatar asked Apr 09 '26 02:04

flofreelance


1 Answers

Actually, your string does not contain backslash, interpreter thinks its a single escape character. Try this solution

console.log(("I M \\ SPLITING").split("\\"));
like image 66
justMe Avatar answered Apr 10 '26 14:04

justMe