Find a way to don't match urls containing @ in regex
I have a regex for validation of urls. But I don't want to match urls containing the character "@". How can 开发者_运维知识库I solve that?
regexp = /((ftp|http|https):\/\/)?(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/;
Well, you can always first check if your url contains @
using indexOf()
, a string method. If it returns a number other than -1
, your url contains @
. If it doesn't, proceed using your regex.
Edit: you can use this code:
if( myString.indexOf('@') == -1 && myString.match(regexp)) {
return true;
}
else {
return false;
}
Which can be further shortened to:
return myString.indexOf('@') == -1 && myString.match(myString);
Thanks goes to @idealmachine for providing this code faster than me.
That should do:
regexp = /((ftp|http|https):\/\/)?[^\/:\s]+(:[0-9]+)?(\/[^@]*)?/;
精彩评论