简体   繁体   中英

Regex to validate url AND does not end in semicolon or space

I have the following regex:

var regex = /https:\/\/company\.zoom\.us\/j\/.*\?pwd=.*/i;

How can I modify this regex to ensure that the url string does not end in a semicolon or space?

https://company.zoom.us/j/xxxxx?pwd=zzzzz //Should pass
https://company.zoom.us/j/xxxxx?pwd=zzzzz  //Should fail (ends in a space after the z)
https://company.zoom.us/j/xxxxx?pwd=zzzzz; //Should fail (ends in a semicolon)
https://company.zoom.us/j/xxxxx?pwd=zzzzz;  //Should fail (ends in a space after the semicolon)

If you don't want to allow whitespaces anywhere and not end with ; then get rid of .* from regex that allows any character.

You may just use:

/^https:\/\/company\.zoom\.us\/j\/\S*\?pwd=[^\s;]*$/

Changes:

  • \\S: : matches 0 or more of any non-whitespace character
  • [^\\s;]* matches any character that is not a ; and not a whitespace

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