0

I have this RegExp: var myReg = RegExp("https?://www.youtube.com/watch\?v=[^\"]+",""); to find the youtube link within a string. I then want to make the part of the string matching the RegExp a variable; lets say var url = "part of string matching the RegExp" then I coudl do something like window.location = url; to redirect the browser directly to the video page. Thanks

2 Answers 2

1

You only have to access the first element of the result, if any:

var r = string.match(myReg);
if(r) var url = r[0];

Take care because is you dont find the url, the result will be a null value

Sign up to request clarification or add additional context in comments.

2 Comments

I let you see the full thing, its not working properly look : jsfiddle.net/a99Ln/3
Your regexp doesnt match the url, it returns a null value. I tried with var myReg = /https?:\/\/www\.youtube\.com\/watch\?v=[^"]+/; and worked fine.
0

This should do the trick:

myReg.exec(str);
var url = RegExp.lastMatch;

Udi

3 Comments

I let you see the full thing, its not working properly look : jsfiddle.net/a99Ln/3
it is because the RegExp is not working as you expected. The RegExp evaluation returns null... check your input / RegExp
the Reg does work, if you try to alert(plainText) on my example it works

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.