Skip to content
Snippets Groups Projects
Unverified Commit b1473c2d authored by Paul Rosenzweig's avatar Paul Rosenzweig Committed by GitHub
Browse files

add regexpEscape instead of deprecated RegExp.excape (#10277)

parent 2baa526d
No related branches found
No related tags found
No related merge requests found
...@@ -6,6 +6,8 @@ import { t, ngettext, msgid } from "ttag"; ...@@ -6,6 +6,8 @@ import { t, ngettext, msgid } from "ttag";
import _ from "underscore"; import _ from "underscore";
import cx from "classnames"; import cx from "classnames";
import { regexpEscape } from "metabase/lib/string";
export default class MetadataSchemaList extends Component { export default class MetadataSchemaList extends Component {
constructor(props, context) { constructor(props, context) {
super(props, context); super(props, context);
...@@ -22,7 +24,7 @@ export default class MetadataSchemaList extends Component { ...@@ -22,7 +24,7 @@ export default class MetadataSchemaList extends Component {
this.setState({ this.setState({
searchText: event.target.value, searchText: event.target.value,
searchRegex: event.target.value searchRegex: event.target.value
? new RegExp(RegExp.escape(event.target.value), "i") ? new RegExp(regexpEscape(event.target.value), "i")
: null, : null,
}); });
} }
......
...@@ -8,6 +8,8 @@ import { t, ngettext, msgid } from "ttag"; ...@@ -8,6 +8,8 @@ import { t, ngettext, msgid } from "ttag";
import _ from "underscore"; import _ from "underscore";
import cx from "classnames"; import cx from "classnames";
import { regexpEscape } from "metabase/lib/string";
export default class MetadataTableList extends Component { export default class MetadataTableList extends Component {
constructor(props, context) { constructor(props, context) {
super(props, context); super(props, context);
...@@ -30,7 +32,7 @@ export default class MetadataTableList extends Component { ...@@ -30,7 +32,7 @@ export default class MetadataTableList extends Component {
this.setState({ this.setState({
searchText: event.target.value, searchText: event.target.value,
searchRegex: event.target.value searchRegex: event.target.value
? new RegExp(RegExp.escape(event.target.value), "i") ? new RegExp(regexpEscape(event.target.value), "i")
: null, : null,
}); });
} }
......
...@@ -3,15 +3,17 @@ import _ from "underscore"; ...@@ -3,15 +3,17 @@ import _ from "underscore";
// Creates a regex that will find an order dependent, case insensitive substring. All whitespace will be rendered as ".*" in the regex, to create a fuzzy search. // Creates a regex that will find an order dependent, case insensitive substring. All whitespace will be rendered as ".*" in the regex, to create a fuzzy search.
export function createMultiwordSearchRegex(input) { export function createMultiwordSearchRegex(input) {
if (input) { if (input) {
return new RegExp( return new RegExp(_.map(input.split(/\s+/), regexpEscape).join(".*"), "i");
_.map(input.split(/\s+/), word => {
return RegExp.escape(word);
}).join(".*"),
"i",
);
} }
} }
// prefix special characters with "\" for creating a regex
export function regexpEscape(s) {
const regexpSpecialChars = /[\^\$\\\.\*\+\?\(\)\[\]\{\}\|]/g;
// "$&" in the replacement string is replaced with the matched string
return s.replace(regexpSpecialChars, "\\$&");
}
export const countLines = str => str.split(/\n/g).length; export const countLines = str => str.split(/\n/g).length;
export function caseInsensitiveSearch(haystack, needle) { export function caseInsensitiveSearch(haystack, needle) {
......
import { regexpEscape } from "metabase/lib/string";
describe("regexpEscape", () => {
const testCases = [
["nothing special here", "nothing special here"],
["somewhat ./\\ special", "somewhat \\./\\\\ special"],
[
"extra special ^$\\.*+?()[]{}|",
"extra special \\^\\$\\\\\\.\\*\\+\\?\\(\\)\\[\\]\\{\\}\\|",
],
];
for (const [raw, escaped] of testCases) {
it(`should escape "${raw}" to "${escaped}"`, () => {
expect(regexpEscape(raw)).toEqual(escaped);
});
}
});
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment