通过分解成更小的部分来解释javascript正则表达式
explain javascript regex by breaking into smaller pieces
这是一个从youtube url中提取视频id的函数。
function youtubeLinkParser(url) {
var regExp = /^.*(youtu.be'/|v'/|u'/'w'/|embed'/|watch'?v=|'&v=)([^#'&'?]*).*/;
var match = url.match(regExp);
if (match && match[2].length == 11) {
return match[2];
} else {
return null;
}
}
我是regex的新手,所以有人介意把regex分解成更小的部分并解释它是如何工作的吗?
Yape::Regex::Explain
The regular expression:
(?-imsx:^.*(youtu.be/|v/|u/'w/|embed/|watch'?v=|'&v=)([^#'&'?]*).*)
matches as follows:
NODE EXPLANATION
----------------------------------------------------------------------
(?-imsx: group, but do not capture (case-sensitive)
(with ^ and $ matching normally) (with . not
matching 'n) (matching whitespace and #
normally):
----------------------------------------------------------------------
^ the beginning of the string
----------------------------------------------------------------------
.* any character except 'n (0 or more times
(matching the most amount possible))
----------------------------------------------------------------------
( group and capture to '1:
----------------------------------------------------------------------
youtu 'youtu'
----------------------------------------------------------------------
. any character except 'n
----------------------------------------------------------------------
be/ 'be/'
----------------------------------------------------------------------
| OR
----------------------------------------------------------------------
v/ 'v/'
----------------------------------------------------------------------
| OR
----------------------------------------------------------------------
u/ 'u/'
----------------------------------------------------------------------
'w word characters (a-z, A-Z, 0-9, _)
----------------------------------------------------------------------
/ '/'
----------------------------------------------------------------------
| OR
----------------------------------------------------------------------
embed/ 'embed/'
----------------------------------------------------------------------
| OR
----------------------------------------------------------------------
watch 'watch'
----------------------------------------------------------------------
'? '?'
----------------------------------------------------------------------
v= 'v='
----------------------------------------------------------------------
| OR
----------------------------------------------------------------------
'& '&'
----------------------------------------------------------------------
v= 'v='
----------------------------------------------------------------------
) end of '1
----------------------------------------------------------------------
( group and capture to '2:
----------------------------------------------------------------------
[^#'&'?]* any character except: '#', ''&', ''?' (0
or more times (matching the most amount
possible))
----------------------------------------------------------------------
) end of '2
----------------------------------------------------------------------
.* any character except 'n (0 or more times
(matching the most amount possible))
----------------------------------------------------------------------
) end of grouping
----------------------------------------------------------------------
^.*
一开始,什么都有可能。
然后是下面这些东西之一:
youtu.be/ (that's the intention, but actually the dot can be any char)
v/
u/some letter/
embed/
watch?v=
&v=
上面的东西变成match[1]。
然后是0个或多个非#&还是?这些字符变成match[2]。
/^.*
以任意字符开头
(youtu.be'/|v'/|u'/'w'/|embed'/|watch'?v=|'&v=)
匹配以下任何一个:
- youu *be/<——可能应该是youtube .be
- v/
- u/w/
- 手表吗?v =
, v =
([^ # ', ' ?] *)
后面跟着除# &然后呢?符号
.*/
任意字符
相关文章:
- Appcelerator Titanium:在运行时下载并解释JavaScript代码
- 解释javascript中的+var和-var一元运算符
- 需要解释Javascript regex请求参数解析
- 解释 JavaScript 中数字的行为
- 请解释Javascript图像旋转代码
- 有人可以向我解释javascript getCookie()中while循环的功能吗?
- 有人可以帮助解释JavaScript中的这个正则表达式吗?
- Mozilla不能很好地解释JavaScript
- 任何人都可以解释JavaScript中的reduce函数吗?
- 解释JavaScript和php代码
- 一些帮助解释javascript函数
- 您将如何向初学者解释Javascript中的回调函数
- 有人能用一个真实的例子来解释JavaScript中函数和方法的区别吗
- 解释JavaScript中的变量作用域
- 解释JavaScript安全扫描警告
- 用Rhino在Java中解释JavaScript:暂停/恢复脚本
- 通过分解成更小的部分来解释javascript正则表达式
- 解释Javascript文件Laravel中的变量
- 需要解释javascript中的作用域问题
- 它“安全”吗?使用注释来解释Javascript中的函数