Skip to content

Instantly share code, notes, and snippets.

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 danielnass/c86714ef969a05405bc43fd423c35f02 to your computer and use it in GitHub Desktop.
Save danielnass/c86714ef969a05405bc43fd423c35f02 to your computer and use it in GitHub Desktop.
Regex for searching a React component by their name. Search just for the specific name, excluding extensions.
/* Search only for the specific name, excluding name extensions
** This example below will found
**
** <Anchor>Anchor Text</Anchor>
**
** <Anchor booleanProp>Anchor Text</Anchor>
**
** <Anchor prop="text">Anchor Text</Anchor>
**
** <Anchor prop={localProp}>Anchor Text</Anchor>
**
** <Anchor
** prop1
** prop3="text"
** prop4={test}
** >
** Anchor Text
** </Anchor>
**
** <Anchor theme="icon" icon="xs-sun" />
**
**********************************************
**
** And will not found these below
**
** <AnchorHey>Anchor Text</Anchor>
**
** <AnchorButton booleanProp>Anchor Text</Anchor>
**
** <AnchorMaybe prop="text">Anchor Text</Anchor>
**
** <AnchorStyles prop={localProp}>Anchor Text</Anchor>
**
** <AnchorModal
** prop1
** prop3="text"
** prop4={test}
** >
** Anchor Text
** </Anchor>
**
** <AnchorSelfClosing theme="icon" icon="xs-sun" />
**
** And any other extension
*/
const regex = /<\bAnchor\b(.|\n|>)*?(\/>|<\/\bAnchor\b>)/gi;
const regexSelfClosingComponents = /<\bIcon\b(.|\n|>)*?\/>/gi;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment