Javascript split to split string in 2 parts irrespective of number of spit characters present in string

javascript

Solution

You can use `match` instead of `split`:

str='123&345&678&910';
splited = str.match(/^([^&]*?)&(.*)$/);
splited.shift();
console.log(splited);

output:

["123", "345&678&910"]

Problem

I want to split a string in Javascript using split function into 2 parts. For Example i have string: ``` str='123&345&678&910' ``` If i use the javascripts split, it split it into 4 parts. But i need it to be in 2 parts only considering the first '&' which it encounters. As we have in Perl split, if i use like: ``` ($fir, $sec) = split(/&/,str,2) ``` it split's str into 2 parts, but javascript only gives me: ``` str.split(/&/, 2); fir=123 sec=345 ``` i want sec to be: ``` sec=345&678&910 ``` How can i do it in Javascript.

Original source

Related problems