Skip to content

Instantly share code, notes, and snippets.

@bluepnume
Last active August 7, 2020 18:06
Show Gist options
  • Save bluepnume/081f542ef86be190b45e867dea324846 to your computer and use it in GitHub Desktop.
Save bluepnume/081f542ef86be190b45e867dea324846 to your computer and use it in GitHub Desktop.
/* @jsx regex.node */
import { regex, Regex, RegexGroup as Group, RegexText as Text, RegexWord as Word } from 'jsx-pragmatic';
const Dot = () => {
return <Text>.</Text>;
};
const At = () => {
return <Text>@</Text>;
};
const emailRegex = ({ providers, tlds, optionalLastName = true }) => {
return (
<Regex>
<Word />
<Group optional={ optionalLastName }>
<Dot />
<Word />
</Group>
<At />
<Group union name='provider'>
{ providers.map(provider => <Text>{ provider }</Text>) }
</Group>
<Dot />
<Group union name='tld'>
{ tlds.map(tld => <Text>{ tld }</Text>) }
</Group>
</Regex>
);
};
const email = '[email protected]';
const match = email.match(emailRegex({
optionalLastName: false,
providers: [ 'paypal', 'google', '$mail' ],
tlds: [ 'com', 'org', 'net' ]
}));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment