24
9

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

jsのsplit()で区切り文字を残す

Posted at
  
  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", ":", ":"]
24
9
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
24
9

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?