字符串首次拆分

时间:2017-07-11 08:20:52

标签: javascript angularjs ecmascript-6

我想将下面的输入字符串拆分为输出字符串 输入=' ABC1:ABC2:ABC3:ABC4'
输出= [' ABC1',' ABC2:ABC3:ABC4']

PropertyChanged

4 个答案:

答案 0 :(得分:3)

您可以使用此功能,适用于所有浏览器



var nString = 'ABC1:ABC2:ABC3:ABC4';
var result = nString.split(/:(.+)/).slice(0,-1);
console.log(result);




答案 1 :(得分:2)

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"]

示例:https://jsfiddle.net/4nq1tLye/

答案 2 :(得分:1)

您可以使用indexOfslice



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);




答案 3 :(得分:1)

console.log('ABC1:ABC2:ABC3:ABC4'.replace(':','@').split('@'));