title: regular expression to mask all but the last 4 digits of a social security number
body: <p>I have the following string</p>
  
  <pre><code>ssn = "166557649"
  </code></pre>
  
  <p>And I want to replace it as such:</p>
  
  <pre><code>ssn = "*****7649"
  </code></pre>
  
  <p>When I use curly braces and specify a limit, it unfortunately replaces all the digits with a single "*":</p>
  
  <pre><code>ssn.gsub(/\d{5}/,"*")
   =&gt; "*7649"
  </code></pre>
  
  <p>I am able to use square brackets to replace multiple digits but it ends up replacing all of them:</p>
  
  <pre><code>ssn.gsub(/[0-9]/,"*")
   =&gt; "*********" 
  </code></pre>
  
  <p>I tried this as well:</p>
  
  <pre><code>ssn.gsub(/[0-9]{5}/,"*")
  =&gt; "*7649" 
  </code></pre>
  
  <p>To clarify, I want to replace all but the last 4 digits. In other words, even if social security number had 20 digits (even though this will never happen), then it should replace the first 16 digits with asterick and just leave the last 4 visible. </p>