diff --git a/packages/core/src/index.ts b/packages/core/src/index.ts index a9028dfe..9ab8174f 100644 --- a/packages/core/src/index.ts +++ b/packages/core/src/index.ts @@ -21,6 +21,7 @@ export * from './pasteRules/markPasteRule' export * from './pasteRules/textPasteRule' export * from './utilities/callOrReturn' +export * from './utilities/escapeForRegEx' export * from './utilities/mergeAttributes' export * from './helpers/combineTransactionSteps' diff --git a/packages/core/src/utilities/escapeForRegEx.ts b/packages/core/src/utilities/escapeForRegEx.ts new file mode 100644 index 00000000..e06bd41b --- /dev/null +++ b/packages/core/src/utilities/escapeForRegEx.ts @@ -0,0 +1,4 @@ +// source: https://stackoverflow.com/a/6969486 +export function escapeForRegEx(string: string): string { + return string.replace(/[-/\\^$*+?.()|[\]{}]/g, '\\$&') +} diff --git a/packages/suggestion/src/findSuggestionMatch.ts b/packages/suggestion/src/findSuggestionMatch.ts index fe7be2b9..17dd3894 100644 --- a/packages/suggestion/src/findSuggestionMatch.ts +++ b/packages/suggestion/src/findSuggestionMatch.ts @@ -1,4 +1,4 @@ -import { Range } from '@tiptap/core' +import { Range, escapeForRegEx } from '@tiptap/core' import { ResolvedPos } from 'prosemirror-model' export interface Trigger { @@ -24,11 +24,7 @@ export function findSuggestionMatch(config: Trigger): SuggestionMatch { $position, } = config - // Matching expressions used for later - const escapedChar = char - .split('') - .map(c => `\\${c}`) - .join('') + const escapedChar = escapeForRegEx(char) const suffix = new RegExp(`\\s${escapedChar}$`) const prefix = startOfLine ? '^' : '' const regexp = allowSpaces