LoginSignup
1
0

More than 1 year has passed since last update.

備忘録・任意の文字で区切られた数字の集まり

Last updated at Posted at 2022-03-02

カンマ区切り・空白区切りの両方に対応したい、という需要から。
入力される文字列は、何らかの文字列によって区切られた数字の繰り返し。
負の値は考慮しない。

const reg =/[\d]{1,}/g;
const list1 ="1 2 3".match(reg);   // [1,2,3]
const list2 = "5 66 ,7".match(reg); //[5,66,7]

最長一致を使う流れでした。
これで一致をとれても積極的に使うわけではないですが、この程度で試行錯誤するのもばかばかしいので。
より効率的な方法があるかもしれませんが、考慮しません。

追記

コメント欄で指摘があったので追記。

const reg =/\d+/g;

上記の内容でも結果は同じ。
+は1回以上の繰り返しだから、{1,}と等価。
\dは単体でMatchする条件として機能するから…などなど。

1
0
1

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
1
0