UNPKG

regexplus

Version:

RegExp Plus

111 lines (89 loc) 3.12 kB
var expect = require('expect.js'); var RegExplus = require('../index'); describe('regexplus', function() { it('normal usage with flags', function() { var reg = new RegExplus('a', 'i'); expect(reg.test('a')).to.eql(true); expect(reg.test('A')).to.eql(true); expect(reg.test('b')).to.eql(false); }); it('normal usage without flags', function() { var reg = new RegExplus('a'); expect(reg.test('a')).to.eql(true); expect(reg.test('A')).to.eql(false); expect(reg.test('b')).to.eql(false); }); it('normal group', function() { var reg = new RegExplus('(a)'); expect(reg.test('a')).to.eql(true); expect(reg.test('A')).to.eql(false); expect(reg.test('b')).to.eql(false); }); it('正向预搜索 test', function() { var reg = new RegExplus('a(?=b)'); expect(reg.test('ab')).to.eql(true); expect(reg.test('aA')).to.eql(false); expect(reg.test('b')).to.eql(false); }); it('正向预搜索 match', function() { var reg = new RegExplus('a(?=b)'); var m = reg.exec('ab'); expect(m instanceof Array).to.eql(true); expect(m[0]).to.eql("a"); expect(m.index).to.eql(0); expect(m.input).to.eql('ab'); m = reg.exec('abc'); expect(m instanceof Array).to.eql(true); expect(m[0]).to.eql("a"); expect(m.index).to.eql(0); expect(m.input).to.eql('abc'); m = reg.exec('aB'); expect(m).to.eql(null); m = reg.exec('a'); expect(m).to.eql(null); }); it('正向预搜索不匹配 test', function() { var reg = new RegExplus('a(?!b)'); expect(reg.test('ab')).to.eql(false); expect(reg.test('aA')).to.eql(true); expect(reg.test('b')).to.eql(false); }); it('正向预搜索不匹配 match', function() { var reg = new RegExplus('a(?!b)'); var m = reg.exec('ab'); expect(m).to.eql(null); m = reg.exec('abc'); expect(m).to.eql(null); m = reg.exec('aB'); expect(m instanceof Array).to.eql(true); expect(m[0]).to.eql("a"); expect(m.index).to.eql(0); expect(m.input).to.eql('aB'); m = reg.exec('aBc'); expect(m instanceof Array).to.eql(true); expect(m[0]).to.eql("a"); expect(m.index).to.eql(0); expect(m.input).to.eql('aBc'); m = reg.exec('a'); expect(m instanceof Array).to.eql(true); expect(m[0]).to.eql("a"); expect(m.index).to.eql(0); expect(m.input).to.eql('a'); }); it('named group test', function() { var reg = new RegExplus('@(?<profile>[a-z0-9]+)/(?<repository>[a-z0-9]+)', 'i'); expect(reg.test('@hotoo/regexplus')).to.eql(true); expect(reg.test('@lizzie/blog')).to.eql(true); expect(reg.test('b')).to.eql(false); }); it('named group exec', function() { var reg = new RegExplus('@(?<profile>[a-z0-9]+)/(?<repository>[a-z0-9]+)', 'i'); var m = reg.exec('@hotoo/regexplus'); expect(m.profile).to.eql('hotoo'); expect(m.repository).to.eql('regexplus'); }); it('named group exec not match', function() { var reg = new RegExplus('@(?<profile>[a-z0-9]+)/(?<repository>[a-z0-9]+)', 'i'); expect(reg.exec('b')).to.eql(null); }); });