You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
33 lines
852 B
TypeScript
33 lines
852 B
TypeScript
// Thanks to https://regex101.com
|
|||
export function regexpCapture (str: string, regex: RegExp, maxIterations = 100) {
|
|||
const result: RegExpExecArray[] = []
|
|||
let m: RegExpExecArray
|
|||
let i = 0
|
|||
|
|||
while ((m = regex.exec(str)) !== null && i < maxIterations) {
|
|||
// This is necessary to avoid infinite loops with zero-width matches
|
|||
if (m.index === regex.lastIndex) {
|
|||
regex.lastIndex++
|
|||
}
|
|||
|
|||
result.push(m)
|
|||
i++
|
|||
}
|
|||
|
|||
return result
|
|||
}
|
|||
|
|||
export function wordsToRegExp (words: string[]) {
|
|||
if (words.length === 0) throw new Error('Need words with at least one element')
|
|||
|
|||
const innerRegex = words
|
|||
.map(word => escapeForRegex(word.trim()))
|
|||
.join('|')
|
|||
|
|||
return new RegExp(`(?:\\P{L}|^)(?:${innerRegex})(?=\\P{L}|$)`, 'iu')
|
|||
}
|
|||
|
|||
export function escapeForRegex (value: string) {
|
|||
return value.replace(/[\\^$.*+?()[\]{}|]/g, '\\$&')
|
|||
}
|