I would like to disallow double forward slashes (//) in my regular expression (and thus allow single /), but my solutions doesn't work.
There also has to be NO / at the beginning and ending of a string (this work!):
/^[^/][a-z0-9-/]+[^/]$$/
this allows for example example///example// but NOT/dzkoadokzd///zdkoazaz
Now I want to disallow multiple "/" after each other. I've tried this but it doesn't work (I'm new to regular expressions):
/^[^/]([a-z0-9-]+[/]{1})+[^/]$$/
/
is a meta character in regex. You need to escape it with a backslash ( \\/
)
/^[^\/]([a-z0-9-]+[\/]{1})+[^\/]$/
不知道这是否是必需条件,但是jperovic的正则表达式通过使用[^ /]可以接受任何字符,而不是斜杠/(因此它还会传递类似“ @ abcd / efgh / ijkl /# ”的字符串(注意@和#),但是在正则表达式的其他部分,我们尝试将字符限制为[a-z0-9-]。如果我们想限制整个字符串的字符范围,请检查以下正则表达式。
/^[a-z0-9\-](?:[a-z0-9\-]|\/(?!\/))+[a-z0-9\-]$/
BTW, if you just want to test for slash at the start or end or double elsewhere:
function badSlash(s) {
var re = /^\/|\/\/|\/$/;
return re.test(s);
}
You can add another OR for whatever other patterns you don't want, eg
var re = /^\/|\/\/|\/$|[^a-z0-9\/]/;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.