rechain

1.0.8 • Public • Published

Build Status Coverage Status NPM Downloads

rechain

Regex Chain - A new method for regular expressions.

Installation

npm i rechain --save

Usage

var r = require('rechain');
 
var ipv4 = r()
.a('25[0-5]')
.or('2[0-4][0-9]')
.or('1[0-9][0-9]')
.or('[1-9][0-9]')
.or('[0-9]')
.p()
 
ipv4 = ipv4.a(r('\\.').a(ipv4).p(0,3));
 
console.log(ipv4.regex);  // (?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])(?:\.(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])){3}
console.log(ipv4.is('192.168.1.1')); // true
console.log(ipv4.contain('192.168.1.a 192.168.1.1'));  // true
console.log(ipv4.match('192.168.1.a 192.68.1.1-8.8.8.8.'));  // [ '192.68.1.1', '8.8.8.8' ]
console.log(ipv4.e().test('192.168.1.1'));  // true
 
// check url
var tlds = 'com|cn';
var protocol = '(?:(?:(?:\\w)+:)?\/\/)?';
var auth = '(?:\\S+(?::\\S*)?@)?';
var host = '(?:xn--[a-z0-9\\-]{1,59}|(?:(?:[a-z\\u00a1-\\uffff0-9]+-?){0,62}[a-z\\u00a1-\\uffff0-9]{1,63}))';
var domain = '(?:\\.(?:xn--[a-z0-9\\-]{1,59}|(?:[a-z\\u00a1-\\uffff0-9]+-?){0,62}[a-z\\u00a1-\\uffff0-9]{1,63}))*';
var tld = '(?:\\.(?:xn--[a-z0-9\\-]{1,59}|' + tlds + '+))';
var port = '(?::\\d{2,5})?';
var path = '(?:\/[^\\s]*)?';
 
var url = r().a(ipv4).or().p('localhost').or().a(host).a(domain).a(tld)
var url = r().a(protocol).a(auth).p(url).a(port).a(path)
 
console.log(url.is('http://markzhan.com')); // true

API

.a(re, [m, n]) - <...>[{m,n}]

  • @param{ Mixed } regex string or rechain object
  • @param{ Mixed } {m,n} <- m, m >= 0 OR ? + * {...} {...}?
  • @param{ Number } {m,n} <- n, n >= m
var r = require('rechain');
console.log(r().a('\\d').regex); // \d
console.log(r().a('\\d').is('8')); // true
console.log(r().a(r('\\d')).is('8')); // true

.or([re, [m, n]]) - <|...>[{m,n}]

  • @param{ Mixed } regex string or rechain object
  • @param{ Mixed } {m,n} <- m, m >= 0 OR ? + * {...} {...}?
  • @param{ Number } {m,n} <- n, n >= m
var r = require('rechain');
console.log(r('hello').or('world').regex); // hello|world
console.log(r('hello').or('world').match('hello')); // [ 'hello' ]
console.log(r('hello').or('world').match('world')); // [ 'world' ]

.m([re, [m, n]]) - <[...]>[{m,n}]

  • @param{ Mixed } regex string or rechain object
  • @param{ Mixed } {m,n} <- m, m >= 0 OR ? + * {...} {...}?
  • @param{ Number } {m,n} <- n, n >= m
var r = require('rechain');
console.log(r().m('0-7').regex); // [0-7]
console.log(r().m('0-7').is('8')); // false
console.log(r().m(r('0-9')).is('8')); // true

.p([re, [m, n]]) - <(...)>[{m,n}]

  • @param{ Mixed } regex string or rechain object
  • @param{ Mixed } {m,n} <- m, m >= 0 OR ? + * {...} {...}?
  • @param{ Number } {m,n} <- n, n >= m
var r = require('rechain');
console.log(r().p('abc').regex); // (?:abc)
console.log(r().p('abc').contain('123')); // false
console.log(r().p(r('abc')).contain('abc123')); // true

.n(m, n) - {m,n}

  • @param{ Mixed } {m,n} <- m, m >= 0 OR string : ? + * {...} {...}?
  • @param{ Number } {m,n} <- n, n >= m
var r = require('rechain');
console.log(r('\\d').n('+').regex); // \d+
console.log(r('\\d').n(3,3).regex); // \d{3,3}
console.log(r('\\d').n('+').match('99')); // [ '99' ]
console.log(r('\\d').n(3,3).match('99.999')); // [ '999' ]

.exact()

var r = require('rechain');
console.log(r('\\d').regex); // \d
console.log(r('\\d').contain('8.8')); // true
console.log(r('\\d').exact().contain('8.8')); // false

.go([opts])

var r = require('rechain');
console.log(r('\\d').regex); // String: \d
console.log(r('\\d').go().regex); // Regexp: /\d/
console.log(r('\\d').go('ig').regex); // Regexp: /\d/gi

.opt(opts)

var r = require('rechain');
console.log(r('\\d').opt('ig').go().regex); // Regexp: /\d/gi
console.log(r('\\d').opt('g').go('i').regex); // Regexp: /\d/i

.e([str])

  • @param { Mixed }
var r = require('rechain');
console.log(r('\\w').e());  // /\w/
console.log(r('\\w').e('ig'));  // /\w/gi
console.log(r('\\w').e('abc').is()); // false
console.log(r('\\w').e('abc').contain()); // true
console.log(r('abc').e('abc').match()); // [ 'abc' ]
console.log(r('\\d').e('abc').match()); // null
console.log(r('\\d').e().test('8'));  // true

.is([str])

  • same as previous

.contain([str])

  • same as previous

.match([str])

  • same as previous

.desc(str)

var r = require('rechain');
console.log(r().desc('API test').description); // 'API test'

License

Copyright © 2015 Mark Zhan.

This project is available under the Apache license. See LICENSE for details.

Package Sidebar

Install

npm i rechain

Weekly Downloads

9

Version

1.0.8

License

Apache 2.0

Last publish

Collaborators

  • markzhan