Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New Coder
    Join Date
    Jan 2011
    Posts
    42
    Thanks
    1
    Thanked 0 Times in 0 Posts

    replace string with asterisk

    I would like to replace all the characters within a string with asterisk using javascript.

    Ex: If i have a string "test123". Replaced string should be "*******".

    Could you please let me know how i can implement this using Regex within javascript.

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,906
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    Code:
    <script type = "text/javascript">
    var str = "test123";
    str = str.replace(/./gi, "*");  // replace each character by an *
    alert (str);
    </script>
    Federer's balls look like water melons out there. - Wimbledon Tennis Commentator Boris Becker, BBC1

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • Users who have thanked Philip M for this post:

    htmlster (07-28-2011)

  • #3
    Regular Coder
    Join Date
    Sep 2010
    Location
    Far far away
    Posts
    122
    Thanks
    0
    Thanked 16 Times in 16 Posts
    Code:
    var str = 'test123';
    
    // create new array with the same number of '*' as the length of the original string
    str = new Array(str.length + 1).join('*');
    
    alert(str);

  • #4
    New Coder
    Join Date
    Jun 2011
    Location
    Algeirs,Algeria
    Posts
    43
    Thanks
    5
    Thanked 3 Times in 3 Posts
    It can be easily done using split() method and for loop:
    Code:
    var x="test123";
    var s="";
    var a=x.split("");//Use an empty string to break on every character.
    for (var i=0;i<a.length;i++)s+="*";

  • #5
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,906
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    The OP asked for a regex. There are of course quite a number of other ways of achieving his objective.

    Code:
    <script type = "text/javascript">
    var str = "test123";
    var len = str.length;
    var repstr = "";
    for (var i = 0; i<len; i++) {
    repstr += "*";
    }
    alert (repstr);
    </script>
    Last edited by Philip M; 07-27-2011 at 06:18 PM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #6
    New to the CF scene
    Join Date
    Jul 2011
    Posts
    5
    Thanks
    0
    Thanked 1 Time in 1 Post
    Code:
    var str = 'test123';

    // create new array with the same number of '*' as the length of the original string
    str = new Array(str.length + 1).join('*');

    alert(str);


    try this it may be some what help full


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •