简体   繁体   中英

javascript regex for string starting with forward slash followed by alphanum chars and no space

需要一个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

This should do it. This takes az and AZ and 0-9.

/^\/[a-z0-9]+$/i

regexper

Image from Regexper.com

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM