0

I'm making a bot in JavaScript for discord and one of it's commands will be custom commands that can give roles,

so I have a string from this: "{role:ROLE NAME}" and I would like to parse "ROLE NAME" out of this, I've looked into RegEx but I'm a little bit stuck

I was wondering if anyone has any suggestions, Thanks

EDIT: Just to say the { is just a symbol not JSON, it could be % or £

8
  • Please post the pattern you are stuck with. Commented Jan 23, 2017 at 23:16
  • Possible duplicate of iterating through json object javascript Commented Jan 23, 2017 at 23:16
  • @dave it's not a JSON object though, the symbol could change to something like %role: Role name% Commented Jan 23, 2017 at 23:16
  • Why you dont use split? Commented Jan 23, 2017 at 23:17
  • @WiktorStribiżew I don't have one thats why I was asking for help. Commented Jan 23, 2017 at 23:17

1 Answer 1

1

You could parse out "ROLE NAME" with this regexp: /:(.*?)\W$/. Or you could parse it with String#split and String#slice like this: s.split(":")[1].slice(0, -1).

Working codepen

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

3 Comments

@JosephBanks I should note that the regexp won't work if "}" appears in the role name. The split method should work with any pair as long as the key "role" doesn't contain ":".
Thanks I'll doubt anyone would put } in a role name but I'll try fix anyway
I also edited it to assume the end of the string could be any non-word character (such as % or £).

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.