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
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,915
    Thanks
    56
    Thanked 545 Times in 542 Posts

    more regex drama

    hello,

    apparently javascript doesn't allow this:
    Code:
    /(?<=\d).*/
    (I get an invalid group error)

    but all I want is to replace everything after the first number, not including the number. I just can't seem to get my head around regex. Any hints?

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,918
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    Code:
    var str= "abc123xyz";
    str = str.replace(/(.*?\d+).*/i, "$1");  
    alert (str);

    The new radio telescope will be the biggest in history, and will enable huge strides in gastronomy. - Reporter BBC Radio 1

    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.

  • #3
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,915
    Thanks
    56
    Thanked 545 Times in 542 Posts
    thanks, Philip. It's not exactly what I had in mind, though - using your example, say I wanted to replace the xyz (although xyz changes) with def. I could do:
    Code:
    str = str.replace(/(.*?\d+).*/i, "$1")+"def"
    but isn't there some funkier way of doing it?

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,918
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    Code:
    <script type = "text/javascript">
    
    var str= "abc123xyz";
    str = str.replace(/(.*?\d+).*/i, "$1def");  
    alert (str);
    
    </script>
    Not sure how to make this "funkier".

    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.

  • #5
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,915
    Thanks
    56
    Thanked 545 Times in 542 Posts
    that's funky enough, thanks


  •  

    Posting Permissions

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