Skip to content Skip to sidebar Skip to footer

Javascript Split String With Backslash

I get from server some path like that: \some\some\some\some\mainSome And for display it to front, I need only last path(mainSome). And try to split it, but I can't. const path = '

Solution 1:

try this

String.raw`\some\some\some\some\mainSome\`.split("\\");

Solution 2:

It's actually escaping the ss and ms in the string - you need to have a string with actual backslashes (escaped like \\):

const path = "\\some\\some\\some\\some\\mainSome".split("\\").pop();
console.log(path);

Solution 3:

If you are ok to add one more slash, it will work

const path = '\\some\\some\\some\\some\\mainSome';
 const splitted = path.split('\\');

return the splitted variable gives you the array set.

Solution 4:

You need to escape backslashes (with backslashes) in your string too;

const path = '\\some\\some\\some\\some\\mainSome'.split('\\');
 
 console.log(path);

Solution 5:

const url = 'some/some/some/mainsom';
const a = url.split('/');
const aGet = a[a.length-1];

Post a Comment for "Javascript Split String With Backslash"