diff --git a/addons/common/CfgFunctions.hpp b/addons/common/CfgFunctions.hpp index 9366f9803..0f4043466 100644 --- a/addons/common/CfgFunctions.hpp +++ b/addons/common/CfgFunctions.hpp @@ -136,6 +136,7 @@ class CfgFunctions { PATHTO_FNC(waitUntilAndExecute); PATHTO_FNC(compileFinal); PATHTO_FNC(createUUID); + PATHTO_FNC(escapeRegex); }; class Broken { diff --git a/addons/common/fnc_escapeRegex.sqf b/addons/common/fnc_escapeRegex.sqf new file mode 100644 index 000000000..1e6cd3ebf --- /dev/null +++ b/addons/common/fnc_escapeRegex.sqf @@ -0,0 +1,27 @@ +#include "script_component.hpp" +/* ---------------------------------------------------------------------------- +Function: CBA_fnc_escapeRegex + +Description: + Escapes special characters used in regex from a string + +Parameters: + _string - String to sanitize + +Returns: + Safe string + +Examples: + (begin example) + "\Q.*?AK-15.*?\E" call CBA_fnc_escapeRegex; + (end) + +Author: + LinkIsGrim +---------------------------------------------------------------------------- */ +SCRIPT(escapeRegex); + +params [["_string", "", [""]]]; + +// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions#escaping +_string regexReplace ["[.?*+^$[\]\\(){}|-]/gio", "\\$&"]