views:

61

answers:

3

Hello! 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 complaininge 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&amp;hl=sv_SE&amp;fs=1&amp;rel=0"&gt;&lt;/param&gt;&lt;param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/ueZP6ifzqMY&amp;hl=sv_SE&amp;fs=1&amp;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? :)

+3  A: 

Javascript doesn't have named capture. Use

param\s+name\s*=\s*"movie"\s+value=\s*"(.*?)"
KennyTM
A: 

Thanks for the fast answer! The named capture error is no more but i still can't get it to validate though.

It still works fine in regex designer and i get a match on the url. But not when i run it in javascript. I use the following code to validate.

var myregexp = new RegExp('param\s+name\s*=\s*"movie"\s+value=\s*"(.*?)"');
if (!$("#textbox-id").val().match(myregexp)) {
   errors += 'Youtube embedden code is wrong\n';
   $("#textbox-id").addClass("epluss-editor-input-error");
}  

I use the same object as above when trying to validate.

Hello user. Please edit new information into your question, don't add an answer. Thanks, and welcome to Stack Overflow.
Kobi
I see you have jQuery in your website. Consider my answer.
Tomalak
+3  A: 

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&amp;hl=sv_SE&amp;fs=1&amp;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
Thanks, i will give this a go. Not as failsafe as a regularexpression but it does the job.
@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
Maybe so, no need to be rude :) Thanks for the answer though.
user27... he's not being rude -- he's just pointing out that you do, in fact, need a reality-check in that regard.
J-P
@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