Reputation: 1
Is there any reason the following string should fail the regular expression below?
String: "http://devices/"
Expression:
/^(http:\/\/|https:\/\/|ftp:\/\/|www.|pop:\/\/|imap:\/\/){1}([0-9A-Za-z]+\.)/.test(input.val())
Thank you for your consideration.
Upvotes: 0
Views: 42
Reputation: 70722
Yes it will fail because of the last dot .
in your regular expression.
/^ ... \.)/
^^
There is not one in the string you are validating against.
http://devices
^ Should be a dot, not a forward slash
If you are planning on using regex to do this, I would probably prefer using a RegExp Object to avoid all the escaping, or group the prefixes together using a non-capturing group.
/^((?:https?|ftp|pop|imap):\/{2}|www\.) ... $/
Upvotes: 1
Reputation: 177
You need to close the regex with a $
.
On this two last: .)
, this dot should be optional, as it is needed to validade.
to satisfy this "http://devices/"
the regex in java at least is:
^((http://)|(https://)|(ftp://)|(pop://)|(imap://)){1}(www.)?([0-9A-Za-z]+)(.)?([0-9A-Za-z]+)?$
Are those /
at the beggining and the end code delimiters?
Upvotes: 0
Reputation: 30985
The reason why it's failing is because, you are using:
^(http:\/\/|https:\/\/|ftp:\/\/|www.|pop:\/\/|imap:\/\/){1}([0-9A-Za-z]+\.)
and you should use:
^(http:\/\/|https:\/\/|ftp:\/\/|www.|pop:\/\/|imap:\/\/){1}([0-9A-Za-z]+.)
You don't have to escape . --------^
Upvotes: 0
Reputation: 1069
The last character in the string must be a period. see "\." at the end of the regex.
You can use http://rubular.com/ to test simple regex expressions and what their matches are.
Upvotes: 0