0

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.

2
  • 1
    This should show you why regexper.com/… Commented Aug 7, 2014 at 16:36
  • ^^^ that was the coolest thing I've seen all day Commented Aug 7, 2014 at 16:39

4 Answers 4

1

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

Live Demo

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\.)  ...  $/
Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

0

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 . --------^

Comments

0

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?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.