findRangesWithRegex.ts
TLDR
This file contains a single function called findRangesWithRegex
that accepts a text and a regular expression as parameters and returns an array of number ranges.
Methods
findRangesWithRegex
The findRangesWithRegex
method is a function that takes a text and a regular expression as input and returns an array of number ranges. It uses the exec
method of the regular expression to find matches in the text, and for each match, it adds a range to the ranges
array. The range consists of the starting index and the ending index of the match in the text. The method continues to find matches until no more matches are found. Finally, it returns the ranges
array.
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
const findRangesWithRegex = (text: string, regex: RegExp): number[][] => {
const ranges: number[][] = []
let matches
do {
matches = regex.exec(text)
if (matches) {
ranges.push([matches.index, (matches.index + matches[0].length) - 1])
}
} while (matches)
return ranges
}
export default findRangesWithRegex