Working in JavaScript I have strings that might contain words prefixed by an "@". The string might look like this @one two @three
. I want a RegExp that finds the words prefixed by the "@" but I don't want to include the "@" itself.
I created a RegExp looking like this /@(\w+)/g
but when I match it against my strings the "@" is indeed included. Why is this? I assumed that if I wanted to include it then the "@" would be inside the capturing parentheses like this /(@\w+)/g
var s = '@one two @three'
var matches = s.match(/@(\w+)/g)
// matches is now [ '@one', '@three' ] but I want [ 'one', 'three' ]
Note with the result I currently get, there is of course no problem in getting the array I want, I can just remove the first character of each string. But what I want to know is:
Is it possible to change my RegExp to get the desired result? And: Why are characters outside my capturing parenthesis included in the result? I find it intuitive that only the characters inside the parenthesis should be included.