views:

62

answers:

1

Hello,

I'm trying to match a string that contains HTML code that contains parameters to a function in Javascript.

There are several of these functions found in the the string containing the HTML code.

changeImg('location','size');

Let's say that I want to grab the location within the single quotes, how would I go about doing this? There are more than one instance in the string.

Thanks in advance.

+3  A: 

This is a fairly common question on SO and the answer is always the same: regular expressions are a poor tool for parsing HTML. Use an XML or HTML parser. That's what they're for. Take a look at Parse HTML With PHP And DOM for an example and Parsing Html The Cthulhu Way for a bit of background.

Parsing Javascript is even harder as it can appear inside <script> tags and attributes so in the very least you'd need to get every <script> tag and parse the contents as well as every element and parse their event handlers (onclick, etc).

I'm reminded of this quote:

"Some people, when confronted with a problem, think "I know, I’ll use regular expressions." Now they have two problems." -- Jamie Zawinski

cletus
That's technically not the rigt quote... but oh well. I forgot where i read that it is a misquote.
RCIX
@RCIX: here is the true source http://regex.info/blog/2006-09-15/247
cletus
Also: "If all you have is a hammer, everything looks like a nail" anonygrammer.
Don