正则表达式在JavaScript中没有按预期工作 [英] Regex not working as expected in JavaScript
问题描述
我写了以下正则表达式:
I wrote the following regex:
(https?:\/\/)?([da-z\.-]+)\.([a-z]{2,6})(\/(\w|-)*)*\/?
其行为可以在这里看到: http://gskinner.com/RegExr/?34b8m
Its behaviour can be seen here: http://gskinner.com/RegExr/?34b8m
我写了以下JavaScript代码:
I wrote the following JavaScript code:
var urlexp = new RegExp(
'^(https?:\/\/)?([da-z\.-]+)\.([a-z]{2,6})(\/(\w|-)*)*\/?$', 'gi'
);
document.write(urlexp.test("blaaa"))
它返回 true
即使正则表达式不允许单个单词有效。
And it returns true
even though the regex was supposed to not allow single words as valid.
我做错了什么?
推荐答案
您的问题是JavaScript正在查看所有转义序列作为字符串的转义符。所以你的正则表达式会记忆如下:
Your problem is that JavaScript is viewing all your escape sequences as escapes for the string. So your regex goes to memory looking like this:
^(https?://)?([da-z.-]+).([a-z]{2,6})(/(w|-)*)*/?$
当您认为文字句点变为正则表达式通配符时,您可能会注意到中间会出现问题。您可以通过几种方式解决此问题。使用正斜杠正则表达式语法JavaScript提供:
Which you may notice causes a problem in the middle when what you thought was a literal period turns into a regular expressions wildcard. You can solve this in a couple ways. Using the forward slash regular expression syntax JavaScript provides:
var urlexp = /^(https?:\/\/)?([da-z\.-]+)\.([a-z]{2,6})(\/(\w|-)*)*\/?$/gi
或者通过逃避你的反斜杠(而不是你正在做的正斜线 - 这是专属于你的时候)重新使用 / regex / mod
表示法,就像你不必在双引号字符串中转义单引号一样,反之亦然):
Or by escaping your backslashes (and not your forward slashes, as you had been doing - that's exclusively for when you're using /regex/mod
notation, just like you don't have to escape your single quotes in a double quoted string and vice versa):
var urlexp = new RegExp('^(https?://)?([da-z.-]+)\\.([a-z]{2,6})(/(\\w|-)*)*/?$', 'gi')
请注意w之前的双反斜杠 - 也是匹配单词字符所必需的。
Please note the double backslash before the w - also necessary for matching word characters.
关于你的正则表达式本身的几个注释:
A couple notes on your regular expression itself:
[da-z.-]
d
包含在az范围内。除非你的意思是 \d
?在这种情况下,斜杠很重要。
d
is contained in the a-z range. Unless you meant \d
? In that case, the slash is important.
(/(\w|-)*)*/?
我自己对嵌套的Kleene星的疑虑放在一边,你可以把这个交替变成一个字符类,并完全删除终止 /?
,因为尾随的斜杠将由您给出的组匹配。我改写为:
My own misgivings about the nested Kleene stars aside, you can whittle that alternation down into a character class, and drop the terminating /?
entirely, as a trailing slash will be match by the group as you've given it. I'd rewrite as:
(/[\w-]*)*
虽然,也许您只想捕捉非空格字符?
Though, maybe you'd just like to catch non space characters?
(/[^/\s]*)*
无论如何,通过这种方式修改你的正则表达式看起来更像:
Anyway, modified this way your regular expression winds up looking more like:
^(https?://)?([\da-z.-]+)\.([a-z]{2,6})(/[\w-]*)*$
请记住,如果你要使用字符串表示法:双重反斜杠。如果您要使用原生 / regex / mod
表示法(我强烈推荐),请转义正斜杠。
Remember, if you're going to use string notation: Double EVERY backslash. If you're going to use native /regex/mod
notation (which I highly recommend), escape your forward slashes.
这篇关于正则表达式在JavaScript中没有按预期工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!