I’m trying to get the YouTube link and get just it’s Id(which is working nice). I’m trying to make a function to detect if the text entered is a URL or just the Id. Like this:
function youtube_do(video, width, height) {
var make_syntax;
var regexp_url;
regexp_url = /((mailto\:|(news|(ht|f)tp(s?))\://){1}\S+)/;
if(regexp_url.test(video) = false) {
make_syntax = embed_format(youtube_id_extract(video), width, height);
} else {
make_syntax = embed_format(video, width, height);
}
document.writeln(make_syntax);
}
And executing it like this:
<script type="text/javascript" src="js/YouTube.js"></script>
<h1>YouTube Example</h1>
<script type="text/javascript">
youtube_do("http://www.youtube.com/watch?v=VMl_71dqeR8", "640", "390");
</script>
When I try that in the browser I got nothing, so I started to debug and I got this errors:
YouTube.js:22 – SyntaxError: Parse error
YouTube.htm:5 – ReferenceError: Can’t find variable: youtube_do
Where 22 is the exactly line of the if statement. What should I do to correct this?
A few points:
1) Your regular expression is invalid because it contains slashes. This will give an error. You need to escape the slashes.
2) The
{1}in your regular expression is redundant – if you don’t specify a quantifier then {1} is implied. This doesn’t give an error, but for clarity you should omit it.3) It is not necessary to escape
:in a regular expression. This doesn’t give an error, but unnecessary escaping decreases readability, so you should remove the unnecessary backslashes.4) You have unnecessary parentheses in your regular expression. This doesn’t give an error but it makes it more difficult to read, so they should be removed.
After these changes this line looks like this:
5) Also the operator
=performs an assignment. This will give an error. You should useif (!...)instead: