Skip to content

A platform for community discussion. Free, open, simple.

DEV: Add escapeRegExp util (#17051)

David Taylorc054a47d9a21
diff --git a/app/assets/javascripts/discourse-common/addon/utils/escape-regexp.js b/app/assets/javascripts/discourse-common/addon/utils/escape-regexp.js
new file mode 100644
index 00000000000..150e36be17f
--- /dev/null
+++ b/app/assets/javascripts/discourse-common/addon/utils/escape-regexp.js
@@ -0,0 +1,3 @@
+export default function escapeRegExp(string) {
+ return string.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"); // $& means the whole matched string
+}
diff --git a/app/assets/javascripts/discourse/app/components/quote-button.js b/app/assets/javascripts/discourse/app/components/quote-button.js
index a8a4de73de5..40d13d7af32 100644
--- a/app/assets/javascripts/discourse/app/components/quote-button.js
+++ b/app/assets/javascripts/discourse/app/components/quote-button.js
@@ -22,6 +22,7 @@ import discourseDebounce from "discourse-common/lib/debounce";
import { getAbsoluteURL } from "discourse-common/lib/get-url";
import { next, schedule } from "@ember/runloop";
import toMarkdown from "discourse/lib/to-markdown";
+import escapeRegExp from "discourse-common/utils/escape-regexp";
function getQuoteTitle(element) {
const titleEl = element.querySelector(".title");
@@ -43,10 +44,6 @@ function fixQuotes(str) {
return str.replace(/[\u201C\u201D]/g, '"');
}
-function regexSafeStr(str) {
- return str.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
-}
-
export default Component.extend(KeyEnterEscape, {
classNames: ["quote-button"],
classNameBindings: [
@@ -198,7 +195,7 @@ export default Component.extend(KeyEnterEscape, {
);
if (this._canEditPost) {
- const regexp = new RegExp(regexSafeStr(quoteState.buffer), "gi");
+ const regexp = new RegExp(escapeRegExp(quoteState.buffer), "gi");
const matches = cooked.innerHTML.match(regexp);
if (
diff --git a/app/assets/javascripts/discourse/app/initializers/url-redirects.js b/app/assets/javascripts/discourse/app/initializers/url-redirects.js
index 8496b1f55a4..b17993efc20 100644
--- a/app/assets/javascripts/discourse/app/initializers/url-redirects.js
+++ b/app/assets/javascripts/discourse/app/initializers/url-redirects.js
@@ -1,5 +1,6 @@
import DiscourseURL from "discourse/lib/url";
import { initializeDefaultHomepage } from "discourse/lib/utilities";
+import escapeRegExp from "discourse-common/utils/escape-regexp";
export default {
name: "url-redirects",
@@ -9,7 +10,7 @@ export default {
const currentUser = container.lookup("current-user:main");
if (currentUser) {
const username = currentUser.get("username");
- const escapedUsername = username.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
+ const escapedUsername = escapeRegExp(username);
DiscourseURL.rewrite(
new RegExp(`^/u/${escapedUsername}/?$`, "i"),
`/u/${username}/activity`
diff --git a/app/assets/javascripts/discourse/app/mixins/composer-upload-uppy.js b/app/assets/javascripts/discourse/app/mixins/composer-upload-uppy.js
index 6dce54f5003..f0fed0ba845 100644
--- a/app/assets/javascripts/discourse/app/mixins/composer-upload-uppy.js
+++ b/app/assets/javascripts/discourse/app/mixins/composer-upload-uppy.js
@@ -21,6 +21,7 @@ import {
import { cacheShortUploadUrl } from "pretty-text/upload-short-url";
import bootbox from "bootbox";
import { run } from "@ember/runloop";
+import escapeRegExp from "discourse-common/utils/escape-regexp";
// Note: This mixin is used _in addition_ to the ComposerUpload mixin
// on the composer-editor component. It overrides some, but not all,
@@ -489,7 +490,7 @@ export default Mixin.create(ExtendableUploader, UppyS3Multipart, {
// when adding two separate files with the same filename search for matching
// placeholder already existing in the editor ie [Uploading: test.png...]
// and add order nr to the next one: [Uploading: test.png(1)...]
- const escapedFilename = filename.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
+ const escapedFilename = escapeRegExp(filename);
const regexString = `\\[${I18n.t("uploading_filename", {
filename: escapedFilename + "(?:\\()?([0-9])?(?:\\))?",
})}\\]\\(\\)`;