3 ms·
It does match. It matches the empty string as your output shows. If there were no matches, the result would have been null instead.[1] >> "foobar".match(RegE
by susam 5y ago
It does match. It matches the empty string as your output shows. If there were no matches, the result would have been null instead.[1]
>> "foobar".match(RegExp("foo"))
Array [ "foo" ]
>> "foobar".match(RegExp("bar"))
Array [ "bar" ]
>> "foobar".match(RegExp("baz"))
null
The last example above does not result in a match. All three examples below lead to successful matches:
>> "foobar".match(RegExp(""))
Array [ "" ]
>> "".match(RegExp(""))
Array [ "" ]
>> "plonk".match(RegExp(""))
Array [ "" ]
I am guessing from the output format in your example that you have used Firefox to present your example. I used Firefox too to present my examples, in order to keep the output format consistent in this thread. If you are indeed using Firefox, you could try expanding the Array result to inspect the property named index in the result. For the first two examples in this comment, the index property would be set to 0 and 3, respectively. For the last three examples (including your "plonk" example), the index property would be set to 0, thus indicating a match at index 0.
[1] https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/match#return_value https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...
- weavie 5y agoAh, yes of course! I am using Firefox. Thanks for the tip, it's been a little while since I've had to do any work with browsers.