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 £

reee
  • 192
  • 2
  • 2
  • 12

1 Answers1

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

SimpleJ
  • 13,812
  • 13
  • 53
  • 93
  • @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 ":". – SimpleJ Jan 23 '17 at 23:24
  • Thanks I'll doubt anyone would put } in a role name but I'll try fix anyway – reee Jan 23 '17 at 23:25
  • I also edited it to assume the end of the string could be any non-word character (such as % or £). – SimpleJ Jan 23 '17 at 23:27