let str = 'a1:b2:c3:d4:e5:f6::';
console.log(str.split(':')); //普通
// ["a1", "b2", "c3", "d4", "e5", "f6", "", ""]
console.log(str.split(/[1-9]/g));//正規表現
//["a", ":b", ":c", ":d", ":e", ":f", "::"]
console.log(str.split(/(:)/g));//区切り文字も一つの要素として分割する
//["a1", ":", "b2", ":", "c3", ":", "d4", ":", "e5", ":", "f6", ":", "", ":", ""]
console.log(str.split(/(?<=:)/g));//区切り文字を直前の要素に含める
//["a1:", "b2:", "c3:", "d4:", "e5:", "f6:", ":"]
console.log(str.split(/(?=:)/g));//区切り文字を直後の要素に含める
//["a1", ":b2", ":c3", ":d4", ":e5", ":f6", ":", ":"]
More than 5 years have passed since last update.
Register as a new user and use Qiita more conveniently
- You get articles that match your needs
- You can efficiently read back useful information
- You can use dark theme