Just doing an indexOf check could still match "requires", "required", "requirejs", etc. By using a conservative regexp we can decrease the likelihood of a false positive. (And I think a cached regexp is faster than indexOfhttp://jsperf.com/substring-test/3).
cc: @substack
I had a better regexp for the common "require" case with no "isRequire" - which is why the test is so crazy. Let me know if you're open to it:
/(^|\W)require(\s*|\/\*[^*]*\*\/|\/\/[^\n]*)*\(/
^ ^ ^ ~~~~~~~~~~~~~~~~~~~~~~~~~~ ^ ^
| | | | |
| | | | + open parens
| | | v
| | + (spaces | block comments | line comments) any number of times
| + our word
+ start or anything not [a-zA-Z_]
Just doing an
indexOf
check could still match "requires", "required", "requirejs", etc. By using a conservative regexp we can decrease the likelihood of a false positive. (And I think a cached regexp is faster thanindexOf
http://jsperf.com/substring-test/3).cc: @substack
I had a better regexp for the common "require" case with no "isRequire" - which is why the test is so crazy. Let me know if you're open to it: