1

I want to create a regex to match URLs that start with http://, https://, // or to find urls that have an extension different from html, htm, php and php3. URL query substrings are optional

Let's say that I want to find these:

http://example.com
/example.mp3
/example.mp3?q=example
http://example.com/example.mp3
#example

And to reject these:

example
/example
/example/
/example.htm
/example.htm?q=example
/example.mp3/example //The .mp3 needs to be extension to be accepted
/example#example

I already tried this /(^(http:\/\/|https:\/\/|\/\/|#)|(.*)((.*)\.^(?!html|htm|php|php3)$)(\?.*)?$)/igm but it didn't worked.

If the opposite(reversing the accepted and declined lists) is easier to do, even that is very appreciated, I can change the function that handles the regex.

9
  • It's not clear why you want to find #example but not /example#example Commented Dec 11, 2016 at 19:37
  • Your example and description of accepted parameters is slightly different. Do you want to accept / or //? Commented Dec 11, 2016 at 19:38
  • @Oriol #example can be an anchor to an id in the same page and /example#example can be an anchor to and id in another page Commented Dec 11, 2016 at 19:40
  • @wizebin i dont want to accept // (cross-origin) and to accept / (same-origin) Commented Dec 11, 2016 at 19:40
  • 1
    @WiktorStribiżew Thank you so much ! It works perfect! Commented Dec 11, 2016 at 20:10

1 Answer 1

1

It seems you may use

^(?:#.+|(?:https?:/)?/[^?#\n]*\.(?!(?:html?|php3?)\b)\w+(?:\?.*)?)$

See the regex demo

Pattern details:

  • ^ - start of string
  • (?:#.+ - either a # followed with any 1+ chars
  • | - or
  • (?:https?:/)?/[^?#\n]*\.(?!html?|php3?)\w+(?:\?.*)?) -
    • (?:https?:/)?/ - an optional http:/ or https:/ and then /
    • [^?#]* - 0+ chars other than ? and #
    • \. - a dot
    • (?!(?:html?|php3?)\b)\w+ - 1 or more letters/digits/underscore that is not equal to htm, html, php or php3
    • (?:\?.*)?) - an optional ? followed with any 0+ chars
  • $ - end of string
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for the answer! The code seemed a little bit harder at the first sight but the explications made me to understand the code way better.

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.