If i perform this instruction in the node repl
"hello".match(/(\w+)(.*)/)
It returns this
[ 'hello',
'hello',
'',
index: 0,
input: 'hello' ]
I expected it to return the first three items, where did the other values come from?
hello, of course.(\w+). That's hello, again.(.*). That's the empty string after hello.index is the position of the start of the match - which is the first character of the string.input shows you the string that the regex was performed on - which is hello.It's surprisingly difficult to find docs on this (at least for me), but here's something from MSDN that describes the object returned by a regex match: http://msdn.microsoft.com/en-us/library/ie/7df7sf4x(v=vs.94).aspx:
If the global flag is not set, the array returned by the match method has two properties,
inputandindex. Theinputproperty contains the entire searched string. Theindexproperty contains the position of the matched substring within the complete searched string.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With