https://github.com/dawsbot/regs
Useful regular expressions for JavaScript
https://github.com/dawsbot/regs
Last synced: 4 months ago
JSON representation
Useful regular expressions for JavaScript
- Host: GitHub
- URL: https://github.com/dawsbot/regs
- Owner: dawsbot
- License: mit
- Created: 2016-03-23T00:42:21.000Z (over 10 years ago)
- Default Branch: master
- Last Pushed: 2019-04-19T11:31:10.000Z (about 7 years ago)
- Last Synced: 2025-10-03T05:00:09.723Z (9 months ago)
- Language: JavaScript
- Size: 31.3 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
- Contributing: .github/contributing.md
- License: license
Awesome Lists containing this project
README
# regs
[](https://www.npmjs.com/package/regs)
[](http://npmjs.org/regs)
[](https://github.com/sindresorhus/xo)
> Useful regular expressions for JavaScript
## Install
```
npm install --save regs
```
## Usage
#### Node
```js
const regs = require('regs');
regs.email().test('me@gmail.com');
//=> true
regs.yeoman().exec('<% var1 %>')[1]
//=> 'var1'
```
#### Web
```html
alert(regs.trim().exec(' var1 ')[1]);
//=> 'var1'
```
## Supported regexp types
* yeoman (`regs.yeoman()`)
* trim (`regs.trim()`)
* email (`regs.email()`)
* githubIssue (`regs.githubIssue()`)
* markdownHeader (`regs.markdownHeader()`)
## API
* Each function call returns a `RegExp object` which can then operate on/with.
* Each function call supports (*optionally*) the [official RegExp flags](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp) which alter behavior of the search. (ex: 'g' for global or 'i' to ignore case)
### regs.yeoman([RegExpFlags])
Capture text between `<% %>` or `<%= %>`.
`1` capture group - the value between delimeters.
Example:
```js
regs.yeoman().exec('<% capture this %>')[1];
//=> 'capture this'
```
### regs.trim([RegExpFlags])
Capture text without surrounding spaces.
`1` capture group - the value between starting and ending spaces.
Example:
```js
regs.trim().exec(' var1 ')[1];
//=> 'var1'
```
### regs.email([RegExpFlags])
Capture all three parts of an email address. Example:
`3` capture groups -
1. Name (before `@`)
2. Domain body (between `@` and `.`)
3. Domain suffix (`com`, `io`, etc.)
Example:
```js
// Simple validation
regs.email().test('name@domain.suffix');
//=> true
// Capture all parts
regs.email().exec('hi@me.io');
//=> [ 'hi@me.io', 'hi', 'me', 'io', index: 0, input: 'hi@me.io' ]
```
### regs.githubIssue([RegExpFlags])
Capture number following a pound (GitHub issue reference)
`1` capture group - The numeric value of the issue
Example:
```js
regs.githubIssue().exec('#98')[1];
//=> '98'
```
### regs.markdownHeader(headerNumber [, RegExpFlags])
Capture text following a markdown header pound (`#`)
`1` capture group - Text following the header
`headerNumber` examples:
* An `h1` in markdown is `# `, and the `headerNumber` should be set to 1
* An `h3` in markdown is `### `, and the `headerNumber` should be set to 3
Example:
```js
regs.markdownHeader(1).exec('# my header 1')[1];
//=> 'my header 1'
regs.markdownHeader(4).exec('#### my header 4')[1];
//=> 'my header 4'
```
## Similar
* [Big list of JavaScript Regular Expressions](https://regex101.com/#javascript)
* [Perl and PHP Regular Expressions](https://gist.github.com/nerdsrescueme/1237767)
## License
MIT © [Dawson Botsford](http://dawsonbotsford.com)