Roll20 uses cookies to improve your experience on our site. Cookies enable you to enjoy certain features, social sharing functionality, and tailor message and display ads to your interests on our site and others. They also help us understand how our site is being used. By continuing to use our site, you consent to our use of cookies. Update your cookie preferences .
×
Create a free account

RegExp constructor doesn't work in a nested function, complex replace()

1590174978

Edited 1590175103
Hi, all... I am validating user supplied input. The first step is working fine. Their supplied value, dropped toLowerCase(), has to be in the array of: const valLoc = [ "head", "hand", "arm", "shoulder", "chest", "stomach", "vitals", "thigh", "leg", "foot", "headshot", "highshot", "bodyshot", "lowshot", "legshot", "random", "none", "hands", "arms", "shoulders", "thighs", "legs","feet", ]; If they supply something else, my variable gets defaulted to "random." If they supply one of the plural versions, however (hands, feet, arms, etc.), I want to return the singular version. The following version of doing that works (assume input is what the user supplied, and valInput is what I want to return): toSingular = { "hands": "hand", "arms": "arm", "shoulders": "shoulder", "thighs": "thigh", "legs": "leg", "feet": "foot", }; valInput = input.replace(/hands|arms|shoulders|thighs|legs|feet/gi, function (matched) { return toSingular[matched]; }); But, it's cumbersome and error-prone to have to type up the keys again. (Besides, I'd like to do this in another replace operation that has about 50 such keys.) The good news seemed to be that by using Object.keys(...) I could get the keys of that list, join them with a pipe character, and construct a new RegExp out of them. Or, I should be able to: toSingular = {     "hands": "hand",     "arms": "arm",     "shoulders": "shoulder",     "thighs": "thigh",     "legs": "leg",     "feet": "foot", }; valInput = input.replace(new RegExp(Object.keys(toSingular).join("|"),'gi'), function (matched) { return toSingular[matched]; }); I can do this in this fiddle , however, when I try to do it in my roll20 script, my sandbox breaks with a "RegExp is not a constructor" error. I am not super fluent with javascript, so if it matters, the line above is positioned in the script like so: var scriptName = scriptName || (function () {     'use strict,     foo = function () {...},     bar = function () {...},     ...     qux = function (input) {         // code would be here         return valInput;     },     ... }()); I mention that because outside one of these inner functions, the RegExp constructor seems to work fine. What am I missing?
1590175900
The Aaron
Roll20 Production Team
API Scripter
Look to see if you've used RegExp as the name of a variable or function somewhere else in the preceding code.  Javascript will let you declare variables that shadow global names, and it will cause issues like this.
Wow. That was it. Forgotten piece of code, with a variable named back when I thought, "Who would ever have need for more than one regex object? I will name you, 'RegExp.'" And, so, were the doom-seeds sewn. Thank you, Aaron!
1590178301
The Aaron
Roll20 Production Team
API Scripter
Hahahahaha. No problem. =D  Been there, done that... many, MANY times... =D