An open API service indexing awesome lists of open source software.

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

Awesome Lists containing this project

README

          

# regs
[![npm version](https://img.shields.io/npm/v/regs.svg)](https://www.npmjs.com/package/regs)
[![npm download count](http://img.shields.io/npm/dm/regs.svg?style=flat)](http://npmjs.org/regs)
[![XO code style](https://img.shields.io/badge/code_style-XO-5ed9c7.svg)](https://github.com/sindresorhus/xo)




Linux & OSX
Windows












> 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)