需要一个JavaScript正则表达式来验证一个字符串,该字符串应以正斜杠(“/”)开头,后跟不带空格的字母数字字符?
The regex you need is:
/^\/[a-z0-9]+$/i
ie:
^
- anchor the start of the string \\/
- a literal forward slash, escaped [a-z0-9]+
- 1 or more letters or digits. You can also use \\d
instead of 0-9
$
- up to the end of the string /i
- case independent Try following:
/^\/[\da-z]+$/i.test('/123') // true
/^\/[\da-z]+$/i.test('/blah') // true
/^\/[\da-z]+$/i.test('/bl ah') // false
/^\/[\da-z]+$/i.test('/') // false
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.