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?
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With