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 7 of 7
  1. #1
    Regular Coder Krupski's Avatar
    Join Date
    Dec 2010
    Location
    United States of America
    Posts
    505
    Thanks
    39
    Thanked 47 Times in 46 Posts

    How to extract certain parts of a string & place in array?

    Hi all,

    I am trying to parse a string and "pull out" certain parts and place those parts into an array.

    For example, I have a string like this:

    <p><img src="./images/smilies/thumbsdown.gif" alt=":thumbsdown:" /><img src="./images/smilies/smiley31.gif" alt=":yeah:" /><img src="./images/smilies/laughingtohard.gif" alt=":reallylaughing:" /></p>


    ...and what I want to do is extract the parts shown in red and place them in an array so I end up with this:

    var codes = [ ':thumbsdown:', ':yeah:', :reallylaughing:' ];

    I tried using a regex to match the alt=":....:" part (which works) but then I don't know how to extract each match separately and put it into an array.

    I'm sure it's simple (probably uses "join") but it's not coming to me.

    I'll appreciate any help.

    Thanks!

    -- Roger
    "Anything that is complex is not useful and anything that is useful is simple. This has been my whole life's motto." -- Mikhail T. Kalashnikov

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,017
    Thanks
    203
    Thanked 2,538 Times in 2,516 Posts
    You need to give each image an id, then


    Code:
    <p><img id = "pic1" src="./images/smilies/thumbsdown.gif" alt=":thumbsdown:" />
    <img id = "pic2"  src="./images/smilies/smiley31.gif" alt=":yeah:" />
    <img id = "pic3" src="./images/smilies/laughingtohard.gif" alt=":reallylaughing:" /></p>
    
    <script type = "text/javascript">
    var codes = [];
    for (var i=1; i<=3; i++) {
    var pic = "pic" + i;
    codes[i] = document.getElementById(pic).alt;
    alert (codes[i])
    }

    Quizmaster: Which Mediterrnean country, whose name sounds like a musical, owns the island of Rhodes?
    Contestant: Fiddler on the Roof

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #3
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,348
    Thanks
    11
    Thanked 589 Times in 570 Posts
    use split instead:

    Code:
    '<p><img src="./images/smilies/thumbsdown.gif" alt=":thumbsdown:" /><img src="./images/smilies/smiley31.gif" alt=":yeah:" /><img src="./images/smilies/laughingtohard.gif" alt=":reallylaughing:" /></p>'
    
     .split(/[\w\W]+?alt="(:\w+:)"/g).slice(1,-1).filter(Boolean);
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/5/28) IE7:0.1, IE8:5.3, IE11:8.4, IE9:3.2, IE10:3.2, FF:18.2, CH:46, SF:7.9, NON-MOUSE:32%

  • Users who have thanked rnd me for this post:

    Krupski (06-25-2012)

  • #4
    Regular Coder Krupski's Avatar
    Join Date
    Dec 2010
    Location
    United States of America
    Posts
    505
    Thanks
    39
    Thanked 47 Times in 46 Posts
    Quote Originally Posted by rnd me View Post
    use split instead:

    Code:
    '<p><img src="./images/smilies/thumbsdown.gif" alt=":thumbsdown:" /><img src="./images/smilies/smiley31.gif" alt=":yeah:" /><img src="./images/smilies/laughingtohard.gif" alt=":reallylaughing:" /></p>'
    
     .split(/[\w\W]+?alt="(:\w+:)"/g).slice(1,-1).filter(Boolean);
    Works perfectly! Thanks!
    "Anything that is complex is not useful and anything that is useful is simple. This has been my whole life's motto." -- Mikhail T. Kalashnikov

  • #5
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,017
    Thanks
    203
    Thanked 2,538 Times in 2,516 Posts
    It does not work in IE9.

    This is rather long-winded but it does work in all browsers:-

    Code:
    <script type = "text/javascript">
    
    var str  =  '<p><img src="./images/smilies/thumbsdown.gif" alt=":thumbsdown:" /><img src="./images/smilies/smiley31.gif" alt=":yeah:" /><img src="./images/smilies/laughingtohard.gif" alt=":reallylaughing:" /></p>'
    var codes = [];
    
    var spl =str.split(">");
    
    for (var i = 1; i<=spl.length-3; i++) {
    var len = spl[i].length;
    spl[i]= spl[i].substring(0,len-1);
    var c = spl[i].indexOf(":");
    spl[i] = spl[i].substring(c);
    var c = spl[i].lastIndexOf(":");
    spl[i] = spl[i].substring(0,c+1)
    alert (spl[i]);
    codes[i-1] = spl[i];
    }
    
    alert (codes);
    
    </script>
    Last edited by Philip M; 06-25-2012 at 10:00 AM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #6
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,348
    Thanks
    11
    Thanked 589 Times in 570 Posts
    Quote Originally Posted by Philip M View Post
    It does not work in IE9.
    it does for me or i wouldn't have posted it without a disclaimer.
    you can generally assume anything i post works in the current version of all common browsers unless noted...
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/5/28) IE7:0.1, IE8:5.3, IE11:8.4, IE9:3.2, IE10:3.2, FF:18.2, CH:46, SF:7.9, NON-MOUSE:32%

  • #7
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,017
    Thanks
    203
    Thanked 2,538 Times in 2,516 Posts
    Quote Originally Posted by rnd me View Post
    it does for me or i wouldn't have posted it without a disclaimer.
    you can generally assume anything i post works in the current version of all common browsers unless noted...
    Sorry, my mistake, it does indeed work with a <!DOCTYPE> declaration.
    But it does not work in <IE9, and there are still many browsers version <IE9 in use.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.


  •  

    Posting Permissions

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