I have this string : "xvvv 123y"
I wanted to test the replace command
So I created :
"xvvv 123y".match(/(v+).+?([0-9]+)/)
result :
["vvv 123", "vvv", "123"] //let’s remember this array.
All Ok.
-
Now say I want to replace the matching text to group
$1==>"xvvv 123y".replace(/(v+).+?([0-9]+)/,'$1')result :
"xvvvy"Ok. -
Now say I want to replace the matching text to group
$2==>"xvvv 123y".replace(/(v+).+?([0-9]+)/,'$2')result :
"x123y"Ok. -
Now say I want to replace the matching text to group
$0==>["vvv 123", "vvv", "123"] ^ | I mean to -------------"xvvv 123y".replace(/(v+).+?([0-9]+)/,'$0')result :
"x$0y"OOPSS ?
Why it wont allow me replace the matching text to the first indexe group ?
edit
And why it returns the first element if I cant access it ?
The first element in the return value of
matchis the string which represents the full match. This value is accessible through$&: