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,207
    What is it meant to do?
    Yes, I know I'm about as subtle as being hit by a bus..(\\.\ Aug08)
    Yep... I say it like I see it, even if it is like a baseball bat in the nutz... (\\.\ Aug08)
    I want to leave this world the same way I came into it, Screaming, Incontinent & No memory!
    I laughed that hard I burst my colostomy bag... (\\.\ May03)
    Life for some is like a car accident... Mine is like a motorway pile up...

    Problems with Vista? :: Getting Cryptic wid it. :: The 'C' word! :: Whois?

  3. #3
    Join Date
    Dec 2011
    Location
    Centurion, South Africa
    Posts
    792
    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
    JavaScript: Learn | Validate | Compact

  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,264
    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