-1

I have got the following regular expression working just fine in Rad Software Regular Expression designer.

param\s+name\s*=\s*"movie"\s+value=\s*"(?<target>.*?)"

And now I am wondering, how to get this to work in JavaScript. It keeps on complaining about the "target" part. I am trying to validate and get the url from the youtube embed code.

<object width="640" height="385"><param name="movie" value="http://www.youtube.com/v/ueZP6ifzqMY&hl=sv_SE&fs=1&rel=0"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/ueZP6ifzqMY&hl=sv_SE&fs=1&rel=0" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="640" height="385"></embed></object>

How the heck do I get this regex to work with my javascript?

Brad Mace
  • 27,194
  • 17
  • 102
  • 148
noshitsherlock
  • 1,103
  • 2
  • 11
  • 28
  • 7
    Here's an answer that suggest to avoid using javascript for parsing HTML: http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454 – Darin Dimitrov Apr 19 '10 at 08:44

2 Answers2

3

Javascript doesn't have named capture. Use

param\s+name\s*=\s*"movie"\s+value=\s*"(.*?)"
kennytm
  • 510,854
  • 105
  • 1,084
  • 1,005
3

If you already have a JS framework like jQuery in your website, I recommend using it instead of regular expressions:

var movieUrl = $(your_html).find("object param[name=movie]").attr("value");
// "http://www.youtube.com/v/ueZP6ifzqMY&hl=sv_SE&fs=1&rel=0"

There are ways to do something similar with pure DOM JavaScript (if you have no framework), too. They result in slightly more code than regex, but are easier to maintain and less likely to fail.

Tomalak
  • 332,285
  • 67
  • 532
  • 628
  • Thanks, i will give this a go. Not as failsafe as a regularexpression but it does the job. – noshitsherlock Apr 19 '10 at 08:57
  • 1
    @user: "not as failsafe as regular expressions" made me laugh. This is ca. 1000 times as failsafe as regular expressions. At least. You might want to get a reality check on just *how much* regular expressions can't handle HTML. – Tomalak Apr 19 '10 at 08:59
  • Maybe so, no need to be rude :) Thanks for the answer though. – noshitsherlock Apr 19 '10 at 09:03
  • user27... he's not being rude -- he's just pointing out that you do, in fact, need a reality-check in that regard. – James Apr 19 '10 at 09:08
  • @user: I really did not mean to be rude. ;-) I'm just being direct. The amount of time alone you have spent trying to create a regex for this case would have been better spent in exploring jQuery's amazing capabilities to do that kind of work for you. It's the superior approach *and* it comes essentially *free*, as you already have jQuery in your project. – Tomalak Apr 19 '10 at 09:14