Reputation: 503
I want to split the below input string as output string.
Input = 'ABC1:ABC2:ABC3:ABC4'
Output = ['ABC1','ABC2:ABC3:ABC4']
let a = 'ABC1:ABC2:ABC3:ABC4'
a.split(':', 2); // not working returning ['ABC1','ABC2']
Upvotes: 3
Views: 92
Reputation: 13488
console.log('ABC1:ABC2:ABC3:ABC4'.replace(':','@').split('@'));
Upvotes: 1
Reputation: 10356
You can use indexOf
and slice
:
var a = 'ABC1:ABC2:ABC3:ABC4';
var indexToSplit = a.indexOf(':');
var first = a.slice(0, indexToSplit);
var second = a.slice(indexToSplit + 1);
console.log(first);
console.log(second);
Upvotes: 1
Reputation: 8509
let a = 'ABC1:ABC2:ABC3:ABC4'
const head = a.split(':', 1);
const tail = a.split(':').splice(1);
const result = head.concat(tail.join(':'));
console.log(result); // ==> ["ABC1", "ABC2:ABC3:ABC4"]
Example: https://jsfiddle.net/4nq1tLye/
Upvotes: 2
Reputation: 30739
You can use this, works in all browsers
var nString = 'ABC1:ABC2:ABC3:ABC4';
var result = nString.split(/:(.+)/).slice(0,-1);
console.log(result);
Upvotes: 3