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 4 of 4
  1. #1
    Senior Coder chump2877's Avatar
    Join Date
    Dec 2004
    Location
    the U.S. of freakin' A.
    Posts
    2,857
    Thanks
    22
    Thanked 157 Times in 148 Posts

    Question regex and javascript arrays

    I'm having a peculiar problem, but probably simple for the right person...I have the following code:

    Code:
    <HTML>
    <HEAD>
    <TITLE></TITLE>
    
    </HEAD>
    
    <BODY>
    
    <script type="text/javascript">
    <!--
    
    var data = "^^^^^randallj";
    
    regex = /\^\^\^\^\^/g;
    parts = data.split(regex);
    
    document.write("parts[0] = "+parts[0]+"<br><br>parts[1] = "+parts[1]);
    
    -->
    </script>
    
    
    </BODY>
    </HTML>
    I noticed that in IE, I get this output:
    parts[0] = randallj

    parts[1] = undefined
    but in Firefox, I get this output:

    parts[0] =

    parts[1] = randallj
    Why is this, and what am I missing here? And most importantly, how do I fix this problem? I would like the array to be filled as it is in Firefox.

    Thank you.
    Regards, R.J.

    ---------------------------------------------------------

    Help spread the word! Like my YouTube-to-Mp3 Conversion Script on Facebook !! :-)
    [Related videos and tutorials are also available at my YouTube channel and on Dailymotion]
    Get free updates about new software version releases, features, and bug fixes!
    ♪♪ …Need Web Hosting For My YouTube-To-Mp3 Conversion Software? Check Here !!… ♪♪

  • #2
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    You don't need the RegExp to split a string, as the split() method does a global search anyway:

    parts = data.split('^^^^^');
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #3
    Senior Coder chump2877's Avatar
    Join Date
    Dec 2004
    Location
    the U.S. of freakin' A.
    Posts
    2,857
    Thanks
    22
    Thanked 157 Times in 148 Posts
    Thanks, that does fix the problem I was having....

    But I'm still wondering why when the regex is used, the arrays are filled differently in different browsers...it seems like these 2 lines of code should return the same results:

    Code:
    var data = "^^^^^randallj";
    
    regex = /\^\^\^\^\^/g;
    parts = data.split(regex);
    and
    Code:
    var data = "^^^^^randallj";
    
    parts = data.split('^^^^^');
    Regards, R.J.

    ---------------------------------------------------------

    Help spread the word! Like my YouTube-to-Mp3 Conversion Script on Facebook !! :-)
    [Related videos and tutorials are also available at my YouTube channel and on Dailymotion]
    Get free updates about new software version releases, features, and bug fixes!
    ♪♪ …Need Web Hosting For My YouTube-To-Mp3 Conversion Software? Check Here !!… ♪♪

  • #4
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    a Reg Exp is not exactly a string. Or in fact is more than that... Anyway, split() method do not use RegExp. It does not need. It is simply a method that does not fixed with RegExp.
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*


  •  

    Posting Permissions

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