dcsimg
www.webdeveloper.com
Results 1 to 7 of 7

Thread: regex prob

  1. #1
    Join Date
    Nov 2013
    Posts
    44

    regex prob

    why it is showing two i's "ii" instead of a single "i"
    Code:
    <!doctype html>
    <html>
    <head>
    <meta charset="utf-8">
    <title>Untitled Document</title>
    </head>
    
    <body>
    <script>
    var str="-1,.05,+1000,3.1415926535,42.";
    document.write(str);
    
    var chng=str.replace(/[+-]?\d*\.?\d*/g,"i");
    document.write(chng);
    </script>
    </body>
    </html>

  2. #2
    Join Date
    Mar 2007
    Location
    localhost
    Posts
    2,589
    What is it meant to do?
    If your post falls off the page, bump it. ...
    Please remember to wrap any code you have in forum tags:-

    [CODE]...[/CODE] [HTML]...[/HTML] [PHP]...[/PHP]

    If you can't think outside the box, you will be trapped forever with no escape...

  3. #3
    Join Date
    Dec 2011
    Location
    Centurion, South Africa
    Posts
    795
    Expression seems very loose, here is one that actually expects something:

    Code:
    <!doctype html>
    <html>
    <head>
    <meta charset="utf-8">
    <title>Untitled Document</title>
    </head>
    
    <body>
    <script>
    var str="-1,.05,+1000,3.1415926535,42.";
    document.write(str);
    
    var chng=str.replace(/[+-]?[\d\.]+/g,"i");
    document.write(chng);
    document.write('<br />');
    
    </script>
    </body>
    </html>
    Output

    -1,.05,+1000,3.1415926535,42.i,i,i,i,i

  4. #4
    Join Date
    Nov 2013
    Posts
    44
    Quote Originally Posted by \\.\ View Post
    What is it meant to do?
    it will replace the sections separated by comma with "i".actually i am learning regex .

  5. #5
    Join Date
    Nov 2013
    Posts
    44
    Quote Originally Posted by bionoid View Post
    Expression seems very loose, here is one that actually expects something
    what do u actually mean by "very loose"?

  6. #6
    Join Date
    Nov 2013
    Posts
    44
    Quote Originally Posted by bionoid View Post
    Expression seems very loose, here is one that actually expects something
    what do u actually mean by "very loose"?

    var chng=str.replace(/[+-]?[\d\.]+/g,"i");
    do i need to escape the dot inside a character class?

  7. #7
    Join Date
    Oct 2010
    Location
    Versailles, France
    Posts
    1,281
    You do not need.

    In most regex flavors, the only special characters or metacharacters inside a character class are the closing bracket (]), the backslash (\), the caret (^), and the hyphen (-). The usual metacharacters are normal characters inside a character class, and do not need to be escaped by a backslash. To search for a star or plus, use [+*]. Your regex will work fine if you escape the regular metacharacters inside a character class, but doing so significantly reduces readability.
    From Regular-expressuions-infos. (Chapter: Metacharacters Inside Character Classes )

    See this page for Metacharacters (chapter : Special characters)

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

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
  •  
HTML5 Development Center



Recent Articles