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

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,689
    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,968
    Whats wrong with using the more simpler
    Code:
    arrayVar = str.split(",");
    STOP using $ prefix on JavaScript variable names...
    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...

  5. #5
    Join Date
    May 2006
    Location
    Somewhere behind your screen
    Posts
    1,689
    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,689
    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

  8. #8
    Join Date
    Feb 2009
    Location
    UK
    Posts
    144

    Post

    sorry posted to wrong thread
    Last edited by MrRed; 01-07-2015 at 07:05 AM.

  9. #9
    Join Date
    Mar 2007
    Location
    localhost
    Posts
    2,968
    How much are your plums ? You have no price for them...
    STOP using $ prefix on JavaScript variable names...
    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...

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