Simple Regex Language 的 JavaScript 版本

1,230 阅读1分钟

JavaScript implementation of Simple Regex 🎉🎉🎉

npm version Build Status codecov

Because of the JavaScript regex engine, there is something different from Simple Regex

  • NOT support as to assign capture name.
  • NOT support if already had/if not already had
  • NO firstMatch, since in JavaScript lazy means non-greedy (matching the fewest possible characters).

npm install srl

Class SRL accepts a Simple Regex Language string as input, and return the builder for the query.

The builder can agent test/exec method to the generated regex object. Or you can use get() to take the generated regex object.

const SRL = require('srl')
const query = new SRL('letter exactly 3 times')
const regex = query.get() // /[a-z]{3}/

query.test('aaa') // true
query.exec('aaa') // [ 'aaa', index: 0, input: 'aaa' ]

query
    .digit()
    .neverOrMore()
    .mustEnd()
    .get() // /[a-z]{3}[0-9]*$/

Required Node 6.0+ for the ES6 support, Or you can use Babel to support Node below 6.0.

Using Webpack and babel-loader to pack it if want to use in browsers.

First, clone repo and init submodule for test.

SRL-JavaScript depends on Mocha and Istanbul to test code. You can use them like this:

npm install

npm test # test 
npm run coverage # Get coverage locally 

How to write Rules, see: Test-Rules.

SRL-JavaScript is published under the MIT license. See LICENSE for more information.