/
githubmirror
/
marktext
Обзор
Документация
Войти
/
githubmirror
/
marktext
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
develop
packages/muya/src/utils/search.ts
50 строк
1 KB
Ran Luo
feat(muya): migrate TS rewrite to packages/muya alongside legacy muyajs (#4314)
29 май 2026, 15:35
Не верифицирован
29 май 2026, 15:35
0d86641
Код
Авторство
О чём код?
import type { IMatch, ISearchOption } from '../search/types'; import execAll from 'execall'; export function matchString(text: string, value: string, options: ISearchOption) { const { isCaseSensitive, isWholeWord, isRegexp } = options; const SPECIAL_CHAR_REG = /[[\]\\^$.|?*+()/]/g; let SEARCH_REG = null; let regStr = value; let flag = 'g'; if (!isCaseSensitive) flag += 'i'; if (!isRegexp) { regStr = value.replace(SPECIAL_CHAR_REG, (p) => { return p === '\\' ? '\\\\' : `\\${p}`; }); } if (isWholeWord) regStr = `\\b${regStr}\\b`; try { // Add try catch expression because not all string can generate a valid RegExp. for example `\`. SEARCH_REG = new RegExp(regStr, flag); return execAll(SEARCH_REG, text); } catch { return []; } } export function buildRegexValue(match: IMatch, value: string) { const groups = value.match(/(?<!\\)\$\d/g); if (Array.isArray(groups) && groups.length) { for (const group of groups) { const index = Number.parseInt(group.replace(/^\$/, '')); if (index === 0) value = value.replace(group, match.match); else if (index > 0 && index <= match.subMatches.length) value = value.replace(group, match.subMatches[index - 1]); } } return value; }