...

View Full Version : How to extract certain parts of a string & place in array?



Krupski
06-24-2012, 06:58 AM
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

Philip M
06-24-2012, 09:24 AM
You need to give each image an id, then



<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

rnd me
06-24-2012, 02:04 PM
use split instead:


'<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);

Krupski
06-25-2012, 12:12 AM
use split instead:


'<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!

Philip M
06-25-2012, 08:57 AM
It does not work in IE9. :(

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


<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>

rnd me
06-25-2012, 07:21 PM
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...

Philip M
06-25-2012, 09:30 PM
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. :o
But it does not work in <IE9, and there are still many browsers version <IE9 in use.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum