Regex - Get All Characters After Last Slash in Url

Get 5 Characters After Last Slash

You could match until the last occurrence of a / with a capturing group and a quantifier {1,6} to match no or flaged

Note that flaged are 6 characters instead of 5 after the /

This page shows how to get the capturing group from the match using either REGEXP_SUBSTR or REGEXP_REPLACE.

^.*/([^/]{1,6})[^/]*$

Explanation

  • ^.*/ Make sure to match the last occurrence of /
  • ( Capture group 1
    • [^/]{1,6} Match 6 times any char other than /
  • ) Close group 1
  • [^/]* Match 0+ occurrences of any char except /
  • $ End of string

See a regex demo

Regular Expression for getting everything after last slash

No, an ^ inside [] means negation.

[/] stands for 'any character in set [/]'.

[^/] stands for 'any character not in set [/]'.

How to grab the alphanumeric id at the end of a URL after the last forward slash in regex

Seems like some kind of hash based on base64 or md5 (too long for md5), so it can contain a-f or 0-9. I added the \s*, because the third test contains space at the end. But if it was a mistake, just remove the \s* from the expression.

([a-fA-F0-9]+)\s*$

https://regexr.com/604cq

Or to be more exact:

[=\/]([a-fA-F0-9]+)\s*$

If it can contain more characters, you can extend it to:

[=\/]([a-zA-Z0-9]+)\s*$

Grab the end of a URL after the last slash with regex in javascript

The simple method is to use a negated character class as

/[^\/]*$/

Regex Demo

Example

var referrerURL = "http://subdomain.xx-xxxx-x.xxx.url.com/content/assets/750";
alert(referrerURL.match(/[^\/]*$/));
// Output
// => 750

Replace everything after last slash in URL

Try this:

Search for: (.*/)(.*)$

Replace with:: $1authcode123$2

Explanation:

 (.*)       -  Anything inside two parens "()" is called a capture
group. The text that matches the pattern inside the
first set of parens is stored in "capture group 1".
You can use $1 to refer to this capture group when
you do your replace. Likewise $2 will refer to the
second capture group.

. - A period represents ANY CHARACTER.

.* - The asterisk "*" means "zero of more of the precedding
pattern." So .* would mean "zero or more occurrences
of ANY CHARACTER."

(.*/) - zero or more characters followed by a slash
(the results of this capture are stored in $1)

(.*)$ - zero or more chars followed by the end of the string
(the results of this capture are stored in $2)

Screenshot

However, you haven't shared all of the details of your requirements, so I don't know if this answer is complete.

For example, what would you do for a url that has no slashes?

Regex to catch everything after last '/' in URL

Use components(separatedBy:) and get the last component from it, i.e.

url1.components(separatedBy: "/").last //"exmaple1.jpg"
url2.components(separatedBy: "/").last //"11974343961963318467fl.png"
url3.components(separatedBy: "/").last //"01+Seth+Easy+v1.gif"

Or you can use lastPathComponent on the URL instance created using url1, url2 and url3

URL(string: url1)?.lastPathComponent //"exmaple1.jpg"
URL(string: url2)?.lastPathComponent //"11974343961963318467fl.png"
URL(string: url3)?.lastPathComponent //"01+Seth+Easy+v1.gif"

All characters after last slash JavaScript RegEx

Try this code:

var string = 'http://steamcommunity.com/id/user/'
document.body.innerHTML = string.match(/([^/]+)\/$/)[1]

I added a capturing group to capture the user folder.

Since that was the 1st capturing group, I selected the item at index 1 of the returned array from string.match()



Leave a reply



Submit