Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Wildcard and glob matching in JavaScript.
* Creates a RegExp from the given string, converting asterisks to .* expressions,
* and escaping all other characters.
function wildcardToRegExp (s) {
return new RegExp('^' + s.split(/\*+/).map(regExpEscape).join('.*') + '$');
* RegExp-escapes all characters in the given string.
function regExpEscape (s) {
return s.replace(/[|\\{}()[\]^$+*?.]/g, '\\$&');

This comment has been minimized.

Copy link

@KrishnaPG KrishnaPG commented Jun 25, 2020

Very helpful. Thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.