From 2de65ef1ffce492c62c07a4c83a84cf65c4db9fe Mon Sep 17 00:00:00 2001 From: ben-pr-p Date: Wed, 22 May 2019 11:03:47 -0400 Subject: [PATCH] escape regex string before custom fields replacement --- src/lib/scripts.js | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/lib/scripts.js b/src/lib/scripts.js index 7e6b2287c..c1788e73c 100644 --- a/src/lib/scripts.js +++ b/src/lib/scripts.js @@ -60,7 +60,7 @@ export const applyScript = ({ script, contact, customFields, texter }) => { let appliedScript = script; for (const field of scriptFields) { - const re = new RegExp(`${delimit(field)}`, "g"); + const re = new RegExp(escapeRegExp(`${delimit(field)}`), "g"); appliedScript = appliedScript.replace( re, getScriptFieldValue(contact, texter, field) @@ -68,3 +68,7 @@ export const applyScript = ({ script, contact, customFields, texter }) => { } return appliedScript; }; + +function escapeRegExp(string) { + return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); // $& means the whole matched string +}