Created
May 3, 2023 07:47
-
-
Save devlato/36ac28a0046050458fcadd72e6449522 to your computer and use it in GitHub Desktop.
Generate a regexp for a string
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
export const generateRegexp = (str: string): string => { | |
const ranges = uniq( | |
uniq([...str]).map((char) => { | |
if (char.match(/[a-z]/) != null) { | |
return 'a-z'; | |
} | |
if (char.match(/[A-Z]/) != null) { | |
return 'A-Z'; | |
} | |
if (char.match(/[0-9]/) != null) { | |
return '0-9'; | |
} | |
console.log(char); | |
return char; | |
}), | |
); | |
return `/${ranges.join('')}/i`; | |
} | |
const uniq = (values) => Object.keys( | |
values.reduce( | |
(acc, char) => { | |
acc[char] = true; | |
return acc; | |
}, | |
{}, | |
), | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment