Summary
I want to be able to escape strings for usage in regular expressions. The code to do this (taken from the escape-string-regex library) is very short:
escape-string-regex/index.js
// From escape-string-regex:
// https://github.com/sindresorhus/escape-string-regexp/blob/main/index.js
export default function escapeStringRegexp(string) {
if (typeof string !== 'string') {
throw new TypeError('Expected a string');
}
// Escape characters with special meaning either inside or outside character sets.
// Use a simple backslash escape when it’s always valid, and a `\xnn` escape when the simpler form would be disallowed by Unicode patterns’ stricter grammar.
return string
.replace(/[|\\{}()[\]^$+*?.]/g, '\\$&')
.replace(/-/g, '\\x2d');
}
Joplin already has a function that seems to escape strings for use in regular expressions:
Questions
- Can I rely on this function escaping regular expressions in the future? (Is that its intended usage, or is it more specialized?)
- Currently
string-utils-common.js
lacks type definitions. To use its contents in a PR, I would have to add a.d.ts
file or migratestring-utils-common.js
to TypeScript.- I see a one PR at a time guideline. Would it be appropriate to migrate
string-utils-common.js
to TypeScript and documentpregQuote
in a second PR?
- I see a one PR at a time guideline. Would it be appropriate to migrate