Re: [jslint] Preferred convention for multiple regex matches? (UNCLASSIFIED)
- Classification: UNCLASSIFIED
Use a single regex for all possible conditions:
(/(first pattern)|(second pattern)/)
Then you can test with a single condition. The pipe is similar to the JS double pipe operator, so in this example if either the first set or the second set is true then you are done.
Just be wary if you are executing this regex in a loop. Regular expressions are processed via an internal API between the interpreter and the code the interpreter is running on, so you take a performance hit each time you access that API. Regular expressions are faster to process than JS instructions though, so they are best outside of a loop using the "g" switch for global.
Austin Cheney, CISSP