Restriction of caractes at the end of regex

Asked

Viewed 45 times

2

I need to make a regex capture urls that only end with letters or numbers.

I got this regex right here:

(https?)(:\/\/www.site.com.br)(\S){1,}

The way it is, this regex allows urls to end with characters like @, ! , ? , etc.

I tried this using lookbehind but could not. How can I do this character restriction only at the end of regex?

1 answer

2

Try this way:

^https.+[\w]$

The idea is to set anchors at the beginning and end of the file:

  • ^https: Forces the URL to start with https;
  • [\w]$: Requires the URL to end with any numeric alpha character, is the equivalent of [a-zA-Z0-9_];
  • dot (.): Indicates any characters;
  • plus (+): one or more characters

Browser other questions tagged

You are not signed in. Login or sign up in order to post.