0.0.3 • Published 5 years ago

better-regex v0.0.3

Weekly downloads
3
License
MIT
Repository
github
Last release
5 years ago

better-regex

Installation

npm install --save better-regex

Include in your app

<script src="dist/better-regex.umd.js"></script>
const {regex} = window.betterRegex
<script type="module">
  import { regex } from './node_modules/better-regex/dist/better-regex.js' 
</script>
// main.js
import { regex } from 'better-regex'
// node
const { regex } = require('better-regex')

Purpose

Template Tag that allows:

  • composing regular expressions
  • breaking expressions across multiple lines
  • commenting expression parts

Basic Usage:

> const dateRegex = regex`
    (?<year>[0-9]{4})     ## year
    -
    (?<month>[0-9]{2})    ## month
    -
    (?<day>[0-9]{2})      ## day
  `
> const { year, month, day } = dateRegex.exec('2019-01-12').groups

Advanced Usage (composition and config):

> const title = regex`(?<title>mr|mrs|dr|ms)`
> const suffix = regex`(?<suffix>jr|sr)`
> const fullNameRegex = regex({flags: 'i'})`
   ^
   ${title}?                ## get title if exists (Mr, Ms, etc)
   \.?
   [ ]?
   (?<firstName>[a-zA-Z]+)  ## pull firstName out into named group
   [ ]
   (?<lastName>[a-zA-Z]+)   ## pull lastName out into named group
   [ ]?
   ${suffix}?               ## you can compose regex's using normal template literal syntax
   \.?
   $
 `
> const { title, firstName, lastName, suffix } = fullNameRegex.exec(`Mr. Cody Landry Sr`).groups

TODO:

  • Add tests
  • Improve docs
0.0.3

5 years ago

0.0.2

5 years ago

0.0.1

5 years ago