正则表达式解释
我正在查看 tumblr 书签中的代码,很好奇下面的代码做了什么。
try{
if(!/^(.*\.)?tumblr[^.]*$/.test(l.host))
throw(0);
tstbklt();
}
谁能告诉我 if 线正在测试什么?我尝试解码正则表达式但无法做到这一点。
I am looking at the code in the tumblr bookmarklet and was curious what the code below did.
try{
if(!/^(.*\.)?tumblr[^.]*$/.test(l.host))
throw(0);
tstbklt();
}
Can anyone tell me what the if line is testing? I have tried to decode the regex but have been unable to do so.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
最初排除正则表达式的细节,此代码为:
“if not regex.matches(l.host)”或“if l.host does not match this regex”
因此,正则表达式必须正确描述 l.host 文本的内容使条件失败,从而避免抛出错误。
关于正则表达式本身:
这是检查
tumblr
是否存在,但仅在可能存在的以.
结尾的任何字符串之后:,看起来可能是为了检查这一点,但如果是这样,那就是错误的做法。
为此,第一位应该类似于
^(?:[\w-]+\.)?
来捕获字母数字子域(?:
是一个非-捕获组,[\w-]+
至少为 1 个字母数字、下划线或连字符),最后一位应为\.(?:com|net|org)$
或者类似于(?:\.[a-zA-Z]+)+$
,具体取决于 tld 部分可能需要的灵活性。Initially excluding the specifics of the regex, this code is:
"if not regex.matches(l.host)" or "if l.host does not match this regex"
So, the regex must correctly describe the contents of l.host text for the conditional to fail and thus avoid throwing the error.
On to the regex itself:
This is checking for the existence of
tumblr
but only after any string ending in.
that might exist:Yeah, it looked like it might be intended to check that, but if so it's the wrong way to do it.
For that, the first bit should be something like
^(?:[\w-]+\.)?
to capture an alphanumeric subdomain (the?:
is a non-capturing group, the[\w-]+
is at least 1 alphanumeric, underscore or hyphen) and the last bit should be either\.(?:com|net|org)$
or perhaps like(?:\.[a-zA-Z]+)+$
depending on how flexible the tld section might need to be.我试图打破它。不过,我不是正则表达式专家:
if(!/
^(..)?tumblr[^.]$/.test( l.host))这部分并不是真正的正则表达式,而是告诉我们仅在该测试不起作用时才执行 if() 。
if(!/
^(.*\.)?tumblr
[^.]*$/.test(l.host))这部分允许在tumblr 单词只要后跟
.
但都是可选的(请参阅末尾的?
)if(!/^(.*.)?tumblr **
[^.]*$/
**.test(l.host))接下来,它匹配除 .
*$
扩展它以匹配之后的任何字符(因此它不会在 1 之后中断),并且它一直有效到字符串末尾。最后,
.test()
看起来会根据当前主机名或 l.host 包含的任何内容对其进行测试(我不熟悉 tumblr 书签)所以基本上,看起来该部分正在检查请注意,如果主机不属于 tumblr,则抛出该异常。
期待看到我的错误有多大:)
My attempt to break it down. I'm no expert with regex however:
if(!/
^(..)?tumblr[^.]$/.test(l.host))This part isn't really regex but tells us to only execute the if() if this test does not work.
if(!/
^(.*\.)?tumblr
[^.]*$/.test(l.host))This part allows for any characters before the tumblr word as long as they are followed by a
.
But it is all optional (See the?
at the end)if(!/^(.*.)?tumblr**
[^.]*$/
**.test(l.host))Next, it matches any character except the . and it the
*$
extends that to match any character afterwards (so it doesn't break after 1) and it works until the end of the string.Finally, the
.test()
looks to test it against the current hostname or whatever l.host contains (I'm not familiar with the tumblr bookmarklet)So basically, it looks like that part is checking to see that if the host is not part of tumblr, then throw that exception.
Looking forward to see how wrong I am :)