Created
June 17, 2022 14:44
-
-
Save olegchursin/f060360e9f94bd2bb85d4868c03a9b04 to your computer and use it in GitHub Desktop.
Match multiple substrings RegEx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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