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 6 of 6
  1. #1
    New to the CF scene
    Join Date
    Aug 2008
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Javascript Split troubles =(

    Hi, I am having a little difficulty with split. I run into a problem when there are two delimeters in a row

    Here is my ugly regular expression
    contents = strContents.split(/\".\"|\"\n\"|\"\n\n\"/g)

    one of the things it is looking for is "," (quotes included) but when two are in a row ",""," it doesn't give me an empty string for between them

    basically the data is set up like this
    "something","something else","another thing","","yet another thing"

    I want the split to return an array with
    0 - "something"
    1 - "something else"
    2 - "another thing"
    3 - ""
    4 - "yet another thing"


    But it doesn't give me the empty string =( (index 3) and if something is not entered somewhere and there is an empty string it will get skipped and mess up my whole javascript program which is dumping stuff into a DB which won't be good if it messes up...

    Thanks


    p.s. I am using IE because the program is not cross browser compatible
    Last edited by BenWhite; 08-11-2008 at 03:56 PM. Reason: browser

  • #2
    Senior Coder shyam's Avatar
    Join Date
    Jul 2005
    Posts
    1,563
    Thanks
    2
    Thanked 163 Times in 160 Posts
    why not do something simpler like this
    Code:
    // map is not present in IE
    Array.prototype.map = function(cb) {
      var ar = [];
      for ( i = 0; i < this.length; i++ ) {
        ar.push(cb.call(null, this[i], i, this));
      }
      return ar;
    };
    var t = '"something","something else","another thing","","yet another thing"';
    var a = t.split(/,/);
    a = a.map(function(v) { return v.replace(/^"|"$/g, ''); });
    You never have to change anything you got up in the middle of the night to write. -- Saul Bellow

  • Users who have thanked shyam for this post:

    BenWhite (08-11-2008)

  • #3
    New to the CF scene
    Join Date
    Aug 2008
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts
    ummm... ok but I am a little lost =/ It is a little above my head. The whole map thing lost me. Does this take care of the new lines like in the split I wrote with the regular expression? If not how do I put those in there? The data I read from I was trying to split on
    ","
    "\n"
    "\n\n"

    edit: I think I see what you are doing now.. you are splitting on the comma and then removing all the quotes. That could work! could I make the expression /,|\n|\n\n/
    Last edited by BenWhite; 08-11-2008 at 04:59 PM.

  • #4
    New to the CF scene
    Join Date
    Aug 2008
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts
    oh wait, now I know why I can't split on comma.
    "Statement Date","June 20, 2008"

    Some of the info has a comma inside the quotes =(


    //-------------edit-----------------------------
    contents = strContents.split(/\",|\n+/)

    I just searched for a comma that is right after a quote and it did the trick for me with the way you showed me. Thanks
    Last edited by BenWhite; 08-11-2008 at 06:47 PM.

  • #5
    Regular Coder
    Join Date
    Jun 2004
    Posts
    565
    Thanks
    0
    Thanked 18 Times in 18 Posts
    Code:
    var x = '"something","something else","another thing","","yet another thing"';
        
    function getAllMatches(regexp, text)
    {
      var
        res = [];
            
      while(regexp.test(text))
      {
        res.push(RegExp.$1);
        text = RegExp.rightContext;
      }
      return res;
    }
    alert(getAllMatches(/^"(.*?)",?/m, x));
    dumpfi

    Edit: Too late :/
    "Failure is not an option. It comes bundled with the software."
    ....../)/)..(\__/).(\(\................../)_/)......
    .....(-.-).(='.'=).(-.-)................(o.O)...../<)
    ....(.).(.)("}_("}(.)(.)...............(.)_(.))Ż/.
    ŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻ
    Little did the bunnies suspect that one of them was a psychotic mass murderer with a 6 ft. axe.

  • #6
    Master Coder
    Join Date
    Feb 2003
    Location
    Umeċ, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    There's a way to do this, that isn't exactly the most concise way, but it should work pretty well for the purpose and it can be done for pretty much any case you want to do multiple matches (Only exception I can think of is if your pattern allows zero length matches.):
    Code:
    var
        re=/"((?:[^\\"]*(?:\\.)?)+)"/g, // The global flag is necessary for this usage.
        input='"something","something else","another thing","","yet another thing","\\""',
        output=[],
        match;
    while(match=re.exec(input))
        output.push(match[1]);
    re.lastIndex=0; // Reset the lastIndex, this is necessary if you want to reuse the regex.
    alert(output.join('\n'));
    Last edited by liorean; 08-11-2008 at 08:08 PM.
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards


  •  

    Posting Permissions

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