Reputation: 220
I want to split the following string into 2 arrays.
First array from the beginning to '/' and the second from '/' to end.
Also, I've tryied with replace to remove the '/' but returns error.
var str = 'word1-word2/word3+word4'.replace(/\+|/|-/g,' ');
http://jsbin.com/kubezabemu/1/edit?js,console
The wanted output should be like:
var arr1 = ['word1', 'word2'];
var arr2 = ['word3', 'word4'];
Upvotes: 2
Views: 79
Reputation: 17906
this way you get an array that contains unlimited of your expected arrays
var arr = 'word1-word2/word3+word4'.split("/");
var res = [];
for(var i = 0; i < arr.length; i++){
str=arr[i].replace(/\+|-/g,' ');
res.push( str.split(" "));
}
console.log(res)
console output
[["word1", "word2"], ["word3", "word4"]]
Upvotes: 3
Reputation: 59232
Here you go. Split on /
first and then on the respective separators.
var arr = 'word1-word2/word3+word4'.split('/'),
arr1 = arr[0].split(/[+-]/),
arr2 = arr[1].split(/[+-]/);
Upvotes: 3