3 ms·
Presumably, you could also do `RegExp("")` to create a zero length regexp. This doesn't seem to match the standard as this site implies it should: "plonk"
by weavie 5y ago
Presumably, you could also do `RegExp("")` to create a zero length regexp. This doesn't seem to match the standard as this site implies it should:
"plonk".match(RegExp(""))
> Array [ "" ]
- susam 5y agoIt 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.