www.webdeveloper.com
Results 1 to 7 of 7

Thread: [RESOLVED] Regular Expression

  1. #1
    Join Date
    Oct 2012
    Location
    Croatia
    Posts
    255

    resolved [RESOLVED] Regular Expression

    Can somene clarify to me why does the following code:


    Code:
    var str = "Oranges, 12.34$, Apples, 21.15$, Bananas, 512.44$, Plums"
    var regExp = /(, \d+.\d+.,)/g;
    var myarray = str.split(regExp);
    var index;
    			
    for (index in myarray)
    {
    	document.write(myarray[index] + "<br />");
    }

    produces following output:


    Oranges
    , 12.34$,
    Apples
    , 21.15$,
    Bananas
    , 512.44$,
    Plums



    while:


    Code:
    var regExp = /(?:, \d+.\d+.,)/g;

    or

    Code:
    var regExp = /, \d+.\d+.,/g;

    produces:


    Oranges
    Apples
    Bananas
    Plums



    I know that () are capturing parentheses and (? are non-capturing one but how does this exactly apply to the code above???

  2. #2
    Join Date
    Oct 2012
    Location
    Croatia
    Posts
    255
    I think I have just found the answer:

    If separator is a regular expression that contains capturing parentheses, then each time separator is matched the results (including any undefined results) of the capturing parentheses are spliced into the output array. However, not all browsers support this capability.

  3. #3
    Join Date
    May 2006
    Location
    Somewhere behind your screen
    Posts
    1,648
    However, not all browsers support this capability
    looks like a hidden trap
    xxx: Guess Buddhist riddle: "What is the sound of one hand clapping?"
    yyy: facepalm

  4. #4
    Join Date
    Mar 2007
    Location
    localhost
    Posts
    2,331
    Whats wrong with using the more simpler
    Code:
    arrayVar = str.split(",");
    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?

  5. #5
    Join Date
    May 2006
    Location
    Somewhere behind your screen
    Posts
    1,648
    too simple
    xxx: Guess Buddhist riddle: "What is the sound of one hand clapping?"
    yyy: facepalm

  6. #6
    Join Date
    Oct 2012
    Location
    Croatia
    Posts
    255
    Quote Originally Posted by \\.\ View Post
    Whats wrong with using the more simpler
    Code:
    arrayVar = str.split(",");
    Then fruit prices would be also stored in an array and I wanted to remove prices from string and store just fruits.

  7. #7
    Join Date
    May 2006
    Location
    Somewhere behind your screen
    Posts
    1,648
    Code:
    <!DOCTYPE html>
    <html>
    <head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8" />
    <title>RE</title>
    <script>
    var str="Oranges, 12.34$, Apples, 21.15$, Bananas, 512.44$, Plums",re=/[\d\.\$]{1,}/g;
    str=str.replace(re,'').replace(/\s/g,'');
    var myarray=str.split(',');
    for(var i in myarray){
    if(myarray[i].length>0)document.write(myarray[i] + "<br />");
    }
    </script>
    </head>
    <body></body>
    </html>
    xxx: Guess Buddhist riddle: "What is the sound of one hand clapping?"
    yyy: facepalm

Thread Information

Users Browsing this Thread

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

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