LoginSignup
3
0

More than 5 years have passed since last update.

文字列の下何桁とか上何桁とかをマスクする

Last updated at Posted at 2018-07-09

下4桁残してあとは*で埋める

'0123456789'.gsub(/.(?=.{4})/, '*') # => "******6789"

?=positive lookaheadだから「先を見てくれ〜」なので、このgsubは「先を見てくれ〜、.{4}あったか?その手前の.*に変えてちょ」である。

上4桁残してあとは*で埋める

'0123456789'.gsub(/(?<=.{4})./, '*') # => "0123******"

?<=positive lookbehindだから「手前を見てくれ〜」なので、このgsubは「手前を見てくれ〜、.{4}あったか?その先の.*に変えてちょ」である。

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