From 63cb5949e155c70805e63aad40e8e727cb2ac2f4 Mon Sep 17 00:00:00 2001 From: Vincent Cantin Date: Mon, 1 Apr 2019 00:17:50 +0800 Subject: [PATCH] Added a utility function which transform a string into another which can be used in a regex without any special matching behavior other than matching the original string. --- src/hopen/syntax/util.cljc | 10 ++++++++++ test/hopen/runner.cljs | 2 ++ test/hopen/syntax/util_test.cljc | 13 +++++++++++++ 3 files changed, 25 insertions(+) create mode 100644 src/hopen/syntax/util.cljc create mode 100644 test/hopen/syntax/util_test.cljc diff --git a/src/hopen/syntax/util.cljc b/src/hopen/syntax/util.cljc new file mode 100644 index 0000000..e582f75 --- /dev/null +++ b/src/hopen/syntax/util.cljc @@ -0,0 +1,10 @@ +(ns hopen.syntax.util + (:require [clojure.string :as str])) + +(defn re-quote + "Escapes characters in the string that are not safe to use in a regex. + Function ported to Clojure from https://github.com/google/closure-library/blob/0257667129eded0cb9b86b4701e50c986b5db648/closure/goog/string/string.js#L1016" + [s] + (-> s + (str/replace #"[-()\[\]{}+?*.$\^|,:#" "\\<%%>"))