Skip to content

Instantly share code, notes, and snippets.

@olegchursin
Created June 17, 2022 14:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save olegchursin/f060360e9f94bd2bb85d4868c03a9b04 to your computer and use it in GitHub Desktop.
Save olegchursin/f060360e9f94bd2bb85d4868c03a9b04 to your computer and use it in GitHub Desktop.
Match multiple substrings RegEx
function matcMultiple(subject: string, strings: string[]) {
const regexMetachars = /[(){[*+?.\\^$|]/g;
for (let i = 0; i < strings.length; i++) {
strings[i] = strings[i].replace(regexMetachars, '\\$&');
}
const regex = new RegExp('\\b(?:' + strings.join('|') + ')\\b', 'gi');
return subject.match(regex) || [];
}
export function hasSubstring(subject: string, substrings: string[]) {
const regexMatchArr = matcMultiple(subject, substrings);
return !!regexMatchArr.length;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment