locked
how to mask part of strings RRS feed

  • Question

  • User450763225 posted

    I have a kind of strings, for example, "234523452345", I want to mask the characters except the last four characters.  got to use regexp ?

    Sunday, August 7, 2016 1:10 AM

Answers

  • User475983607 posted

    Use substring() and concatenation.

    var num = "234523452345";
    var lastFour = num.substring(num.length-4, 4);
    var maskString = "xxxx-xxxx-xxxx" + lastFour;

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, August 7, 2016 10:22 AM

All replies

  • User283571144 posted

    Hi sdnd2000,

    sdnd2000

    <div id="reference-post-content" class="reply-comment">

    I have a kind of strings, for example, "234523452345", I want to mask the characters except the last four characters.  got to use regexp ?

    </div>

    As far as I know, you could use JavaScript String substr() Method to get the last four characters .

    More details, you could refer to follow link and codes:

    http://www.w3schools.com/jsref/jsref_substr.asp

    Codes:

        <script>
            var str = '234523452345';
            var last4 = str.substr(str.length - 4);
            var result = 'xxxx-xxxx-xxxx-' + last4;
            alert(result);
        </script>

    Best Regards,

    Brando

    Sunday, August 7, 2016 2:45 AM
  • User450763225 posted

    the ending should be variant, not just 2345,  it should be something like this  xxxx-xxxx-xxxx-2345

    Sunday, August 7, 2016 7:41 AM
  • User475983607 posted

    Use substring() and concatenation.

    var num = "234523452345";
    var lastFour = num.substring(num.length-4, 4);
    var maskString = "xxxx-xxxx-xxxx" + lastFour;

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, August 7, 2016 10:22 AM