Regular expressions are powerful, but I find that they’re also incredibly annoying to use.
Recently I was trying to create a parser that would try a few different regular expressions and output an object with the proper fields defined. The input was a string separated by periods, so it was fairly easy to create the expressions themselves:
Note that basically all of the regular expression testing websites will automatically append the /g flag. This flag allows you to match all occurrences instead of the first one. However, if you want to test the regular expression via the .test(str: String) method, the /g flag will also advance the last index, making it so you can’t use the RegExp again without resetting it. I ran in to this problem when I was originally doing a .test before doing the .exec – the .exec would return null, because the RegExp had advanced its index to the end of the string, and no more matches were found.