3

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?

2
  • 1
    You can't think of any way? Commented Mar 19, 2016 at 20:12
  • There aren't any characters after the last slash :) Commented Mar 19, 2016 at 20:17

2 Answers 2

3

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

Sign up to request clarification or add additional context in comments.

2 Comments

Ah just saw your edit... thank you. Will accept your answer when the time allows.
@Garuuk No problem! If this answered your question, please mark it as accepted (press the tick underneath the voting buttons). EDIT: Just saw your comment edit, thanks!
1

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.

2 Comments

Thanks for your input
@Garuuk: My pleasure. Don't forget to check the second method without using capturing groups.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.