I have this string
let str = "name1,name2/name3"
and I want to split on "," and "/", is there is a way to split it without using regexp?
this is the desire output
name1
name2
name3
I have this string
let str = "name1,name2/name3"
and I want to split on "," and "/", is there is a way to split it without using regexp?
this is the desire output
name1
name2
name3
If you don't want to use regex you can use this function:
function split (str, seps) {
sep1 = seps.pop();
seps.forEach(sep => {
str = str.replace(sep, sep1);
})
return str.split(sep1);
}
usage:
const separators = [',', ';', '.', '|', ' '];
const myString = 'abc,def;ghi jkl';
console.log(split(myString, separators));
You can use regexp:
"name1,name2/name3".split(/,|;| |\//);
this will split by ,, ;, or /
or
"name1,name2/name3".split(/\W/)
this will split by any non alphanumeric char