Andrew's Digital Garden

JS regex is weird with strings

When using a string to construct a RegExp, you don't need to escape forward slashes /? You do when creating one with the /regex/ format.

Also, they store their lastIndexOf when using .test and .match when using the g flag. Meaning subsequent tests can produce weird results.

ALSO, you need to double escape things when using the string, as the first one escapes it in the string, and the second escapes it once its converted to a regex format https://stackoverflow.com/questions/17863066/why-do-regex-constructors-need-to-be-double-escaped

Some of this is different with /regex/ creating strings (1 and 3 is only true for strings)

[[js]]

JS regex is weird with strings