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 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Oct 2011
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Check a string for 1 to infinite repetitions of asterisk (*) using JavaScript regex o

    I am completely crap in regex and I need a little help with a task. How do I check a string for a single character repetition (in my case *) using javasctipt regex object. Specifically I need to check a string for 1 to n repetitions of * character. Notice that i need to check a string that contains only asterisks not any other character ex. not validate "tom***" or "**tom" or "*tom*" but "*" or "**" or "**n times". If the above case is true return true. Forgive my "noviceness".

    With respect,

    Tom

    Greece

  • #2
    Senior Coder
    Join Date
    Apr 2011
    Location
    London, England
    Posts
    2,120
    Thanks
    15
    Thanked 354 Times in 353 Posts
    Code:
    var expr = "**a";
    var regex = /[^\*]/g;
    if (!expr.length || expr == '') {
        alert('Empty string');
    } else if (regex.test(expr)) {
        alert('Not just stars');
    } else {
        alert('Just stars.');
    }
    seems to work
    "I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
    Validate your HTML and CSS

  • #3
    Senior Coder jmrker's Avatar
    Join Date
    Aug 2006
    Location
    FL
    Posts
    3,083
    Thanks
    38
    Thanked 498 Times in 492 Posts

    Lightbulb

    Try ...
    Code:
    <!DOCTYPE html>
    <html lang="en">
    <meta charset="utf-8" />
    <title> Untitled </title>
    <script type="text/javascript">
    function countStars(str) {
      alert(str+': '+/^\*+$/.test(str));
    }
    countStars('***')
    countStars('*')
    
    countStars('***tom');
    countStars('tom**');
    countStars('***tom**');
    </script>
    
    <style type="text/css">
    
    </style>
    </head>
    <body>
    
    </body>
    </html>
    Last edited by jmrker; 08-26-2012 at 12:20 AM. Reason: Alternate solution submitted too late because I type too slow! :(

  • #4
    Senior Coder
    Join Date
    Apr 2011
    Location
    London, England
    Posts
    2,120
    Thanks
    15
    Thanked 354 Times in 353 Posts
    I've slightly revised my example as I don't need to escape \ the asterisk as it is within a character class, and the global modifier is unnecessary with test():

    Code:
    var expr = "**";
    var regex = /[^*]/;
    if (!expr.length || expr == '') {
        alert('Empty string');
    } else if (!regex.test(expr)) {
        alert('Just stars.');
    }​
    The regex.test() expression essentially says "return true as soon as you find a character other than an asterisk, otherwise return false".
    Last edited by AndrewGSW; 08-26-2012 at 03:08 AM.
    "I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
    Validate your HTML and CSS

  • #5
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,982
    Thanks
    203
    Thanked 2,536 Times in 2,514 Posts
    Code:
    <script type = "text/javascript">
    
    var str = "***tom***";
    if (/[^\*]/gi.test(str)) {
    alert ("String contains characters other than asterisks");
    }
    
    var str = "*********";
    var len = str.length;
    alert ("There are " + len + " asterisks in your string")
    
    </script>

    The cheese in the mousetrap is always free.

    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.


  •  

    Tags for this Thread

    Posting Permissions

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