user11360001
user11360001

Reputation:

Regex for matching certain url

It's should match those urls

https://example.com/id/username/
http://example.com/id/username/
https://www.example.com/id/username
http://example.com/id/username/

basically it's should start with http or https when maybe www when example.com and /id and last is username which could be anything, and / is not always in end

username could be anything

I got this so far:

  if (input.match(/http\:\/\/example\.com/i)) {
    console.log('-');
  }

also how to check with regex if urls ends with 7 number like 1234567/ or 3523173. / not always in end

Upvotes: 0

Views: 88

Answers (3)

Niladri
Niladri

Reputation: 189

Use the following regular expression

http(s)?:\/\/(www\.)example.com\/id\/[a-zA-Z0-9]+

You can change [a-zA-Z0-9] as per your username format if you required. See following example:

  • [a-zA-Z0-9]+ ==> Username contain Uppercase, Lowercase, Number. (john008)
  • [a-zA-Z]+ ===> Username contain Uppercase, Lowercase. (john)
  • [0-9]+ ===> Username contain only Number. (123456)

Upvotes: 1

Jan
Jan

Reputation: 43169

Without further specification you could use

\bhttps?:.+?example\.com\/[a-zA-Z]+\/\w+\/?(?=\s|\Z)

See a demo on regex101.com.


This is

\b                   # a word boundary
https?:              # http/https:
.+?                  # anything else afterwards, lazily
example\.com         # what it says
\/[a-zA-Z]+\/\w+\/?  # /id/username with / optional
(?=\s|\Z)            # followed by a whitespace or the end of the string

Upvotes: 0

vortex
vortex

Reputation: 852

https?\:\/\/(www\.)?example\.com\/id\/([a-zA-Z]+)\/?

Upvotes: 0

Related Questions