0

I need following URL should be returned as true

Following url should be returned as false

This is regex I tried so far /^(?:http(s)?:\/\/)?[\w.-]+(?:\.[\w\.-]+)+[\w\-\._~:/?#[\]@!\$&'\(\)\*\+,;=.]+$/. Im using this inside angular form input ng-patter

Any help would be appreciated

3
  • looks like you are categorizing get request data as false. Couldn't you just search for "?" Commented Feb 13, 2018 at 10:18
  • try this pattern maybe it'll fit you: /([a-z0-9_\-]{1,5}:\/\/)?(([a-z0-9_\-]{1,}):([a-z0-9_\-]{1,})\@)?((www\.)|([a-z0-9_\-]{1,}\.)+)?([a-z0-9_\-]{3,})(\.[a-z]{2,4})(\/([a-z0-9_\-]{1,}\/)+)?([a-z0-9_\-]{1,})?(\.[a-z]{2,})?(([\?\&][a-z0-9_\-]{1,}\=[a-z0-9_\-]{1,})+)?/gi Commented Feb 13, 2018 at 10:27
  • @BloomBlack anything comes after ? is invalid URL for me Commented Feb 13, 2018 at 10:33

1 Answer 1

1

Try this regular expression

/^((?:http://)|(?:https://))(www.)?((?:[a-zA-Z0-9]+.[a-z]{3})|(?:\d{1,3}.\d{1,3}.\d{1,3}.\d{1,3}(?::\d+)?))([/a-zA-Z0-9.]*)$/gm

function isUrlValid(userInput) {
    var res = userInput.match(/^((?:http:\/\/)|(?:https:\/\/))(www.)?((?:[a-zA-Z0-9]+\.[a-z]{3})|(?:\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}(?::\d+)?))([\/a-zA-Z0-9\.]*)$/gm);
    if(res == null)
        return false;
    else
        return true;
 }
Sign up to request clarification or add additional context in comments.

1 Comment

something is wrong with regex. it is throwing error

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.