DEV: Add escapeRegExp util (#17051)

This was re-implemented in a number of places - it makes more sense as a utility function.
This commit is contained in:
David Taylor 2022-06-10 01:37:54 +01:00 committed by GitHub
parent 3ebfde5ea2
commit c054a47d9a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 9 additions and 7 deletions

View File

@ -0,0 +1,3 @@
export default function escapeRegExp(string) {
return string.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"); // $& means the whole matched string
}

View File

@ -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 (

View File

@ -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`

View File

@ -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])?(?:\\))?",
})}\\]\\(\\)`;