PDA

View Full Version : RegExp.replace Not Working


SamirDarji
01-29-2006, 04:02 AM
I'm using the following function to modify tags used for mouseover text:function delHoverX()
{
re = /(\bdsc00001\b)/i;

imgTags = document.getElementsByTagName("img");
for (i=0; i<imgTags.length; i++)
{
if (re.test(imgTags[i].title))
{
origTitle = imgTags[i].title;
origAlt = imgTags[i].alt;
imgTags[i].title.replace(re, "$1");
imgTags[i].alt.replace(re, "$1");
if (origTitle == imgTags[i].title && origAlt == imgTags[i].alt)
{
imgTags[i].title = "same";
imgTags[i].alt = "same";
}
}
else
{
imgTags[i].title = "";
imgTags[i].alt = "";
}
}
}It is being used on the following web page: http://huntsvillecarscene.smugmug.com/gallery/1144571

For some reason, the regexp.replace is not working. There is a test clause in there to help me diagnose some of what's going on.

I'm not a Javascript programmer. I only have a limited background in C. I've been working on this for 2 days and am frustrated out of my mind.

Any assistance appreciated.

filburt1
01-29-2006, 04:18 AM
If that is a vBulletin-powered page, it will be easier and cleaner to replace in the HTML output before it's even sent to the browser. Do this via a hook in global_complete (IIRC) on the $output variable.

SamirDarji
01-29-2006, 06:36 AM
Unfortunately, this isn't a vb page. :( I don't have almost any control on anything. This was code that was given to me that should work and I've narrowed it down to one test case (dsc00001) just to get it working.

It seems like the .replace isn't working for some reason. In spite of the fact that the re.test works dropping it inside the if.

I've looked at the RegExp documentation for Javascript on devguru as well as other sites. I've looked at examples. Seems like everything is written correctly, but it is still not working. I'm simply dumbfounded. If this was in C, I'd step through the whole thing line-by-line while watching every single variable. But I don't have any tools to do that here. :(

Any ideas?

Marco van Herwaarden
01-29-2006, 08:59 AM
Turn on displaying all JavaScript errors in your browser. This might give you a clue.

SamirDarji
01-29-2006, 02:53 PM
I will try that. Good idea!

Results--no errors related to that code. (There's one error that's there because of a call to a vb object leftover from a cut-and-paste of some vb generated html.)

Any other ideas? I'm playing with using .match now with some better results, but still unusual things are happenning. :(

filburt1
01-29-2006, 03:28 PM
Are you trying to create a title attribute for each image that already has an alt tag?

SamirDarji
03-07-2006, 03:24 AM
That's exactly what I was trying to do. I don't understand why I never got the email from vb.org of your response. :( Thank you for your help!