Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

All characters after last slash JavaScript RegEx

I have this as a string

http://steamcommunity.com/id/user/

And when I use this regex pattern : [^\/]+\/$ I get this result is

user/

I'm trying to negate the last / so i can get

user

How can I accomplish this?

like image 528
Garuuk Avatar asked May 07 '26 17:05

Garuuk


2 Answers

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()

var string = 'http://steamcommunity.com/id/user/'

document.body.innerHTML = string.match(/([^/]+)\/$/)[1]

Demo on JSFiddle

like image 149
Kaspar Lee Avatar answered May 09 '26 07:05

Kaspar Lee


Use the capturing group like this ([^\/]+)\/$ and capture the first group using \1. Demo

OR

Use the lookahead assertion like this ([^\/]+)(?=\/$). Demo

Second method will directly capture user without messing with capturing groups.


Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!