JavaScript和正则表达式的问题 [英] Problems With JavaScript And Regular Expressions

查看:105
本文介绍了JavaScript和正则表达式的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图获取YouTube链接并获取它的ID(这很好用)。我试图做一个函数来检测输入的文本是URL还是只是Id。像这样:

  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);
}

像这样执行它:

 < script type =text / javascriptsrc =js / YouTube.js>< / script> 

< h1> YouTube示例< / h1>
< script type =text / javascript>
youtube_do(http://www.youtube.com/watch?v=VMl_71dqeR8,640,390);
< / script>

当我在浏览器中尝试时,我什么都没有,所以我开始调试,并且我得到了这个错误:


YouTube.js:22 - SyntaxError:解析错误

YouTube.htm:5 - ReferenceError:Can' t找到变量:youtube_do


其中22是的精确行,如果声明。我应该怎么做才能解决这个问题?

解决方案



< 1)你的正则表达式是无效的,因为它包含斜杠。这会给出错误。您需要避开斜线。



<2>正则表达式中的 {1} 是多余的 - 如果您不要指定量词,则隐含{1}。这不会导致错误,但为了清楚起见,您应该省略它。



3)不需要转义在正则表达式中。这不会导致错误,但不必要的转义会降低可读性,所以您应该删除不必要的反斜杠。



4)在正则表达式中有不必要的括号。这不会产生错误,但会使读起来更加困难,因此应该将其删除。



在这些更改后,此行如下所示:

  regexp_url = /(mailto:|(news |(ht | f)tps?):\ / \ /)\S + / ; 

5)运算符 = 执行分配。这会给出错误。您应该使用 if(!...)代替:

  if (!regexp_url.test(video))


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:

regexp_url = /(mailto:|(news|(ht|f)tps?):\/\/)\S+/;

5) Also the operator = performs an assignment. This will give an error. You should use if (!...) instead:

if (!regexp_url.test(video))

这篇关于JavaScript和正则表达式的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆