Skip to content

Commit

Permalink
Common - Add CBA_fnc_escapeRegex (#1640)
Browse files Browse the repository at this point in the history
  • Loading branch information
LinkIsGrim committed Mar 28, 2024
1 parent 18a1699 commit 38a6278
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
1 change: 1 addition & 0 deletions addons/common/CfgFunctions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ class CfgFunctions {
PATHTO_FNC(waitUntilAndExecute);
PATHTO_FNC(compileFinal);
PATHTO_FNC(createUUID);
PATHTO_FNC(escapeRegex);
};

class Broken {
Expand Down
27 changes: 27 additions & 0 deletions addons/common/fnc_escapeRegex.sqf
Original file line number Diff line number Diff line change
@@ -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 <STRING>
Returns:
Safe string <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", "\\$&"]

0 comments on commit 38a6278

Please sign in to comment.