Skip to content

Commit

Permalink
Add quick example section in README
Browse files Browse the repository at this point in the history
  • Loading branch information
hlysine committed Aug 18, 2023
1 parent f0c2d34 commit 2a392a3
Showing 1 changed file with 53 additions and 0 deletions.
53 changes: 53 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ Create readable Regular Expressions with concise and flexible syntax.
- [Installation](#installation)
- [Using a package manager](#using-a-package-manager)
- [Using a CDN](#using-a-cdn)
- [Quick Example](#quick-example)
- [Features](#features)
- [Readability](#readability)
- [Flexibility and Conciseness](#flexibility-and-conciseness)
Expand Down Expand Up @@ -63,6 +64,58 @@ Import readable-regexp via a script tag in your HTML file, then access the `read
const { oneOrMore, exactly } = readableRegExp;
```

## Quick Example

This is an email RegExp:

```js
/^([^<>()[\]\\.,;:@"\s]+(?:\.[^<>()[\]\\.,;:@"\s]+)*|".+")@(\[\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\]|(?:[a-zA-Z0-9\-]+\.)+[a-zA-Z]{2,})$/
```

And this is the same expression written in readable RegExp:

```js
const allowedChar = notCharIn`<>()[]\\\\``.,;:@"`(whitespace);
const email = lineStart
.capture.oneOf
( // username
oneOrMore.match(allowedChar)
.zeroOrMore(
exactly`.`
.oneOrMore.match(allowedChar)
)
)
( // quoted string
exactly`"`
.oneOrMore.char
.exactly`"`
)
.exactly`@`
.capture.oneOf
( // IPv4 address
exactly`[`
.repeat(1, 3).digit
.exactly`.`
.repeat(1, 3).digit
.exactly`.`
.repeat(1, 3).digit
.exactly`.`
.repeat(1, 3).digit
.exactly`]`
)
( // domain name
oneOrMore(
oneOrMore.charIn`a-z``A-Z``0-9``-`
.exactly`.`
)
.atLeast(2).charIn`a-z``A-Z`
)
.lineEnd
.toRegExp();

email.test("[email protected]"); // true
```

## Features

### Readability
Expand Down

0 comments on commit 2a392a3

Please sign in to comment.