├── .gitignore ├── LICENSE ├── README.md ├── component.json ├── lib ├── parse.js ├── parsexp.js ├── regex.js └── reglex.js ├── package.json └── tests └── regex.js /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules/ 2 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2013 Aadit M Shah 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 6 | 7 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 8 | 9 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 10 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Regex # 2 | 3 | Native RegExp compatible regular expressions for JavaScript. Patterns may be composed of subexpressions and multiple expressions may be combined into a single expression. 4 | 5 | ## Installation ## 6 | 7 | Regex may be installed on [node.js](http://nodejs.org/ "node.js") via the [node package manager](https://npmjs.org/ "npm") using the command `npm install regex`. 8 | 9 | You may also install it on [RingoJS](http://ringojs.org/ "Home - RingoJS") using the command `ringo-admin install aaditmshah/regex`. 10 | 11 | You may install it as a [component](https://github.com/component/component "component/component") for web apps using the command `component install aaditmshah/regex`. 12 | 13 | ## Usage ## 14 | 15 | The `Regex` constructor is compatible with the native `RegExp` constructor. You may pass it a `regexp` object or a source string: 16 | 17 | ```javascript 18 | var Regex = require("regex"); 19 | var regex = new Regex(/(a|b)*abb/); 20 | ``` 21 | 22 | ## Methods ## 23 | 24 | Like the native `RegExp` constructor instances of `Regex` have the following methods: 25 | 26 | ### test ### 27 | 28 | The `test` method is used to simply test whether a string matches the regex pattern: 29 | 30 | ```javascript 31 | regex.test("abb"); // true 32 | regex.test("aabb"); // true 33 | regex.test("babb"); // true 34 | regex.test("aaabb"); // true 35 | regex.test("ababb"); // true 36 | regex.test("abba"); // false 37 | regex.test("cabb"); // false 38 | ``` 39 | 40 | ## Operations ## 41 | 42 | The `Regex` constructor currently supports the following regex operations: 43 | 44 | 1. Concatenation 45 | 2. Alternation 46 | 3. Grouping 47 | 4. Closure 48 | 49 | ## Changelog ## 50 | 51 | The following changes were made in this version of Regex: 52 | 53 | ### v0.1.0 ### 54 | 55 | * Supports basic regex operations - concatenation, alternation, grouping and closure. No support for pattern composition or combining subexpressions yet. 56 | -------------------------------------------------------------------------------- /component.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "regex", 3 | "repo": "aaditmshah/regex", 4 | "description": "Native RegExp compatible regular expressions for JavaScript. Patterns may be composed of subexpressions and multiple expressions may be combined into a single expression.", 5 | "version": "0.1.1", 6 | "keywords": ["regex", "regexp", "regular", "expression", "pattern", "definition", "composition", "subexpression", "combination"], 7 | "main": "lib/regex.js", 8 | "scripts": ["lib/regex.js"], 9 | "license": "MIT", 10 | "twitter": "@zcombinator" 11 | } 12 | -------------------------------------------------------------------------------- /lib/parse.js: -------------------------------------------------------------------------------- 1 | if (typeof require === "function") { 2 | var parsexp = require("./parsexp"); 3 | parsexp.lexer = require("./reglex"); 4 | } else parsexp.lexer = reglex; 5 | 6 | var yy = parsexp.yy; 7 | var parse = parsexp.parse.bind(parsexp); 8 | if (typeof module === "object") module.exports = parse; 9 | 10 | yy.atom = function (symbol) { 11 | var start = {}; 12 | var final = {}; 13 | 14 | start[symbol] = [final]; 15 | 16 | return { 17 | start: start, 18 | final: final 19 | }; 20 | }; 21 | 22 | yy.group = function (atom) { 23 | var start = {}; 24 | var final = {}; 25 | 26 | var moves = start[""] = [atom.start, final]; 27 | addMoves(atom.final, "", moves.slice()); 28 | 29 | return { 30 | start: start, 31 | final: final 32 | }; 33 | }; 34 | 35 | yy.sequence = function (sequence, group) { 36 | var start = sequence.start; 37 | var final = group.final; 38 | 39 | var begin = group.start; 40 | var end = sequence.final; 41 | addMoves(begin, "", [end]); 42 | addMoves(end, "", [begin]); 43 | 44 | return { 45 | start: start, 46 | final: final 47 | }; 48 | }; 49 | 50 | yy.expression = function (expression, sequence) { 51 | var start = {}; 52 | var final = {}; 53 | 54 | start[""] = [expression.start, sequence.start]; 55 | addMoves(expression.final, "", [final]); 56 | addMoves(sequence.final, "", [final]); 57 | 58 | return { 59 | start: start, 60 | final: final 61 | }; 62 | }; 63 | 64 | function addMoves(state, symbol, moves) { 65 | var oldMoves = state[symbol]; 66 | state[symbol] = oldMoves ? oldMoves.concat(moves) : moves; 67 | } 68 | -------------------------------------------------------------------------------- /lib/parsexp.js: -------------------------------------------------------------------------------- 1 | if (typeof require === "function") var Parser = require("jison").Parser; 2 | 3 | var parsexp = new Parser({ 4 | "bnf": { 5 | "pattern": [ 6 | ["expression EOF", "return $1;"] 7 | ], 8 | "expression": [ 9 | ["expression | sequence", "$$ = yy.expression($1, $3);"], 10 | ["sequence", "$$ = $1;"] 11 | ], 12 | "sequence": [ 13 | ["sequence group", "$$ = yy.sequence($1, $2);"], 14 | ["group", "$$ = $1;"] 15 | ], 16 | "group": [ 17 | ["atom *", "$$ = yy.group($1);"], 18 | ["atom", "$$ = $1;"] 19 | ], 20 | "atom": [ 21 | ["SYMBOL", "$$ = yy.atom($1);"], 22 | ["( expression )", "$$ = $2;"] 23 | ] 24 | } 25 | }); 26 | 27 | if (typeof module === "object") module.exports = parsexp; 28 | -------------------------------------------------------------------------------- /lib/regex.js: -------------------------------------------------------------------------------- 1 | if (typeof require === "function") { 2 | var StateMachine = require("statemachines"); 3 | var parse = require("./parse"); 4 | require("augment"); 5 | } 6 | 7 | var nfa = Function.bindable(StateMachine.Nondeterministic, null); 8 | if (typeof module === "object") module.exports = Regex; 9 | Regex.convert = convert; 10 | Regex.parse = parse; 11 | 12 | function Regex(regexp) { 13 | var graph = parse(regexp instanceof RegExp ? regexp.source : regexp); 14 | var dfa = (new (nfa.apply(null, convert(graph)))).subset(); 15 | this.test = dfa.test.bind(dfa); 16 | } 17 | 18 | function convert(graph) { 19 | var states = [graph.start]; 20 | var transition = []; 21 | var cursor = 0; 22 | 23 | while (cursor < states.length) { 24 | var state = states[cursor++]; 25 | var symbols = Object.keys(state); 26 | var length = symbols.length; 27 | var tuple = {}; 28 | 29 | for (var i = 0; i < length; i++) { 30 | var symbol = symbols[i]; 31 | var moves = state[symbol]; 32 | var degree = moves.length; 33 | var transitions = []; 34 | 35 | for (var j = 0; j < degree; j++) { 36 | var move = moves[j]; 37 | var index = states.indexOf(move); 38 | 39 | if (index < 0) { 40 | index = states.length; 41 | states.push(move); 42 | } 43 | 44 | transitions.push(index); 45 | } 46 | 47 | tuple[symbol] = transitions; 48 | } 49 | 50 | transition.push(tuple); 51 | } 52 | 53 | return [transition, [states.indexOf(graph.final)]]; 54 | } 55 | -------------------------------------------------------------------------------- /lib/reglex.js: -------------------------------------------------------------------------------- 1 | if (typeof require === "function") var Lexer = require("lex"); 2 | 3 | var reglex = new Lexer; 4 | 5 | reglex.addRule(/[(|)*]/, function (lexeme) { 6 | return lexeme; 7 | }); 8 | 9 | reglex.addRule(/./, function (lexeme) { 10 | this.yytext = lexeme; 11 | return "SYMBOL"; 12 | }); 13 | 14 | reglex.addRule(/$/, function () { 15 | return "EOF"; 16 | }); 17 | 18 | if (typeof module === "object") module.exports = reglex; 19 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "regex", 3 | "description": "Native RegExp compatible regular expressions for JavaScript. Patterns may be composed of subexpressions and multiple expressions may be combined into a single expression.", 4 | "version": "0.1.1", 5 | "keywords": ["regex", "regexp", "regular", "expression", "pattern", "definition", "composition", "subexpression", "combination"], 6 | "author": "Aadit M Shah (http://aaditmshah.github.com/) ", 7 | "main": "lib/regex.js", 8 | "scripts": { 9 | "test": "node tests/regex.js" 10 | }, 11 | "maintainers": [{ 12 | "name": "Aadit M Shah", 13 | "email": "aaditmshah@fastmail.fm", 14 | "web": "http://aaditmshah.github.com/" 15 | }], 16 | "contributors": [{ 17 | "name": "Aadit M Shah", 18 | "email": "aaditmshah@fastmail.fm", 19 | "web": "http://aaditmshah.github.com/" 20 | }], 21 | "bugs": "https://github.com/aaditmshah/regex/issues", 22 | "licenses": [{ 23 | "type": "MIT", 24 | "url": "http://opensource.org/licenses/MIT" 25 | }], 26 | "repositories": [{ 27 | "type": "git", 28 | "url": "https://github.com/aaditmshah/regex.git" 29 | }], 30 | "dependencies": { 31 | "lex": "1.7.4", 32 | "jison": "0.4.4", 33 | "augment": "3.2.1", 34 | "statemachines": "0.1.0" 35 | }, 36 | "homepage": "https://github.com/aaditmshah/regex" 37 | } 38 | -------------------------------------------------------------------------------- /tests/regex.js: -------------------------------------------------------------------------------- 1 | var Regex = require(".."); 2 | 3 | var regex = new Regex(/(a|b)*abb/); 4 | var regex2 = new Regex(/[a-b]*abb/); 5 | var regex3 = new Regex(/.*abb/); 6 | var regex4 = new Regex(/.*/); 7 | 8 | if (regex.test("abb") && 9 | regex.test("aabb") && 10 | regex.test("babb") && 11 | regex.test("aaabb") && 12 | regex.test("ababb") && 13 | regex2.test("aabb") && 14 | regex3.test("aabb") && 15 | regex4.test("aabb") && 16 | !regex.test("abba") && 17 | !regex.test("cabb")) console.log("Passed all tests."); 18 | else { 19 | console.error("Failed test(s)."); 20 | process.exit(1); 21 | } 22 | --------------------------------------------------------------------------------