2

I am trying to use regex in a jQuery function to select and mask all characters in a string with an'x' except the first 4 and last 4 characters. The string can be any length. I can successfully mask the last 4 digits and first 4 digits separately but I don't really understand regex well enough to select the nth character in a string to the nth character and mask them. If anybody can help it would be very grateful - I have spent many hours trawling around forums and trying to write my own regex but to no avail.

Thanks

My current function looks like this:

<input type="text" class="read-only-mask" title="" value="1233434343434456789012" name="" id=""readonly />
<script>
$(document).ready(function() {
    $('.read-only-mask').val(function(_,val) {
         return val.replace(/.(?=.{4})/g, 'x');     
    });
});         
</script>

This would show 1233434343434456789012 as xxxxxxxxxxxxxxxxxx9012

I need it to show as 1233xxxxxxxxxxxxxx9012 but the string could be any length so 123343434343 would need to show as 1233****4343 etc

4
  • 3
    why use regex or any other thing for this simple loop? for(var i = 4; i < 10; i++) { string[i] = maskChar; } Commented Jun 8, 2016 at 15:25
  • @RichardBarker, I really think these types of problems can be summed up by a friend of mine. He once said that we as engineers build solutions that challenge our intellect. I am very guilty of this. Thank you for pointing out the obvious. Add it as an answer and I'll vote for you. Commented Jun 8, 2016 at 15:27
  • @Jamiec, while his exact algorithm may not have worked, the simplicity by which it was produced is the importance. It may have to copy the data to another string, but that's no big deal. Commented Jun 8, 2016 at 15:28
  • Also, have you tried jquery input.mask plugin ? Commented Jun 8, 2016 at 15:30

5 Answers 5

2

you are far better off using a simpler approach. Save your self some time and headache and use the KISS method.

var maxMaskLength = 10;
var minMaskLength = 4;
$(document).ready(function() {
    $('.read-only-mask').val(function(_, val) {
  var valSplit = val.split("");
  for (i = minMaskLength; i < maxMaskLength; i++)
    valSplit[i] = 'x';
  return valSplit.join("");
   });
 });
Sign up to request clarification or add additional context in comments.

Comments

2

I would prefer to do it like this

var str = "1233434343434456789012",
    res = Array.prototype.map.call(str, (e,i) => i > 4 && i < 9 ? "X" : e).join("");
console.log(res);

    var str = "1233434343434456789012",
        res = Array.prototype.map.call(str, (e,i,a) => i < a.length-4 ? "X" : e).join("");
    console.log(res);

whichever best fits your application.

Comments

1

I would use this way:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" class="read-only-mask" title="" value="1233434343434456789012" name="" id=""readonly />
<script>
  $(document).ready(function() {
    $('.read-only-mask').val(function(_, val) {
      val = val.split("");
      for (i = 4; i < val.length; i++)
        val[i] = 'x';
      return val.join("");
    });
  });         
</script>

For the above input, it shows 1233xxxxxxxxxxxxxxxxxx. If that's what you need?

Comments

1

You really dont need regex for this, all you're doing is 3 substrings

  • string start to the mask start (1)
  • mask start to mask end (2)
  • mask end to string end. (3)

You then form the string back together by concatenating (1) above, the mask char to the length of (2) and finally (3).

var input = "1234567890";

var output = input.substring(0,4) + (Array(3).join('*')) + input.substring(6,10)

console.log(output)

Comments

1

You can use capturing parentheses:

 "111122222333".replace( /(.{4})(.{5})(.*)/, '$1xxxxx$3');
  • 1111 will be $1, replaced by itself.
  • 22222 will be $2, replaced by xxxxx.
  • 333 will be $3, replaced by itself.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.