├── .gitattributes ├── test └── test.js ├── .gitignore ├── package.json ├── MIT-LICENSE.txt ├── README.md └── index.js /.gitattributes: -------------------------------------------------------------------------------- 1 | * text=auto -------------------------------------------------------------------------------- /test/test.js: -------------------------------------------------------------------------------- 1 | var assert = require('assert') 2 | var regexp = require('../') 3 | var equal = assert.equal 4 | 5 | console.log('Start...') 6 | var re = regexp() 7 | .start('http') 8 | .maybe('s') 9 | .must('://') 10 | .maybe('WWW.') 11 | .somethingBut(regexp.space) 12 | .end('.com') 13 | .ignoreCase() 14 | .toRegExp() 15 | 16 | equal(re.test("http://qq.com"), true) // => true 17 | equal(re.test("http://www.qq.com"), true) // => true 18 | equal(re.test("https://www.qq.com"), true) // => true 19 | equal(re.test("http://www.qqcom"), false) // => false 20 | equal(re.test("https://www.qq.net"), false) // => false 21 | 22 | console.log('Done.') -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Include your project-specific ignores in this file 2 | # Read about how to use .gitignore: https://help.github.com/articles/ignoring-files 3 | 4 | # Numerous always-ignore extensions 5 | *.diff 6 | *.err 7 | *.orig 8 | *.log 9 | *.rej 10 | *.swo 11 | *.swp 12 | *.vi 13 | *~ 14 | *.sass-cache 15 | 16 | # OS or Editor folders 17 | .DS_Store 18 | ._* 19 | Thumbs.db 20 | .cache 21 | .project 22 | .settings 23 | .tmproj 24 | nbproject 25 | *.sublime-project 26 | *.sublime-workspace 27 | .idea 28 | 29 | # Dreamweaver added files 30 | _notes 31 | dwsync.xml 32 | 33 | # Komodo 34 | *.komodoproject 35 | .komodotools 36 | 37 | # Espresso 38 | *.esproj 39 | *.espressostorage 40 | 41 | # Rubinius 42 | *.rbc 43 | 44 | # Folders to ignore 45 | .hg 46 | .svn 47 | .CVS 48 | 49 | # Node 50 | npm-debug.log 51 | node_modules 52 | 53 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "node-regexp", 3 | "description": "New RegExp Style for Node.js", 4 | "version": "0.1.1", 5 | "homepage": "https://github.com/yuanyan/node-regexp", 6 | "author": { 7 | "name": "yuanyan", 8 | "url": "https://github.com/yuanyan" 9 | }, 10 | "repository": { 11 | "type": "git", 12 | "url": "git://github.com/yuanyan/node-regexp.git" 13 | }, 14 | "bugs": { 15 | "url": "https://github.com/yuanyan/node-regexp/issues" 16 | }, 17 | "licenses": [ 18 | { 19 | "type": "MIT", 20 | "url": "https://github.com/yuanyan/node-regexp/blob/master/MIT-LICENSE.txt" 21 | } 22 | ], 23 | "bin": "", 24 | "engines": { 25 | "node": ">= 0.6.0" 26 | }, 27 | "scripts": { 28 | "test": "node test/test.js" 29 | }, 30 | "dependencies": { 31 | }, 32 | "devDependencies": { 33 | }, 34 | "keywords": [] 35 | } -------------------------------------------------------------------------------- /MIT-LICENSE.txt: -------------------------------------------------------------------------------- 1 | Copyright (c) 2013 yuanyan 2 | 3 | Permission is hereby granted, free of charge, to any person 4 | obtaining a copy of this software and associated documentation 5 | files (the "Software"), to deal in the Software without 6 | restriction, including without limitation the rights to use, 7 | copy, modify, merge, publish, distribute, sublicense, and/or sell 8 | copies of the Software, and to permit persons to whom the 9 | Software is furnished to do so, subject to the following 10 | conditions: 11 | 12 | The above copyright notice and this permission notice shall be 13 | included in all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 16 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 17 | OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 18 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 19 | HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 20 | WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 21 | FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 22 | OTHER DEALINGS IN THE SOFTWARE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | node-regexp 2 | === 3 | 4 | New RegExp Style for Node.js 5 | 6 | ## Install 7 | ```sh 8 | $ npm install node-regexp 9 | ``` 10 | 11 | ## API 12 | 13 | ### Methods 14 | ```js 15 | var regexp = require('node-regexp') 16 | var re = regexp(). 17 | .start('str') // must start str 18 | .maybe('str') // maybe match str 19 | .atleast(3) // atleast match 3 times 20 | .must('str') // must match str 21 | .has(1, 5) // should have 1 to 5 22 | .either('str1', 'str2', 'str3') // either str1, str2, str3 23 | .find('str') // capture match 24 | .anythingBut('str') // anything match but str 25 | .somethingBut('str') // something match but str 26 | .end('str') // match end str 27 | .global() // global match 28 | .ignoreCase() // ignore case match 29 | .multiline() // multiple lines match 30 | .toRegExp() // return a RegExp 31 | .toString() // return a String 32 | ``` 33 | 34 | ### Vars 35 | ```js 36 | var regexp = require('node-regexp') 37 | regexp.number = "[0-9]" 38 | regexp.lower = "[a-z]" 39 | regexp.upper = "[A-Z]" 40 | regexp.letter = "[a-zA-Z]" 41 | regexp.tab = "\\t" 42 | regexp.space = "\\s" 43 | regexp.word = "\\w" 44 | regexp.digit = "\\d" 45 | regexp.newline = "\\n" 46 | regexp.return = "\\r" 47 | regexp.eol = "(?:(?:\\n)|(?:\\r\\n))" 48 | ``` 49 | 50 | ## Demo 51 | 52 | ```js 53 | var regexp = require('node-regexp') 54 | var re = regexp() 55 | .start('http') 56 | .maybe('s') 57 | .must('://') 58 | .maybe('WWW.') 59 | .somethingBut(regexp.space) 60 | .end('.com') 61 | .ignoreCase() 62 | .toRegExp() 63 | 64 | re.test("http://qq.com") // => true 65 | re.test("http://www.qq.com") // => true 66 | re.test("https://www.qq.com") // => true 67 | re.test("http://www.qqcom") // => false 68 | re.test("https://www.qq.net") // => false 69 | ``` -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | var defaultSource = "(?:)" 2 | 3 | function toArray(arg){ 4 | return [].slice.call(arg) 5 | } 6 | 7 | function regexp () { 8 | if(this.constructor !== regexp) return new regexp() 9 | this.source = defaultSource 10 | } 11 | 12 | regexp.number = "[0-9]" 13 | regexp.lower = "[a-z]" 14 | regexp.upper = "[A-Z]" 15 | regexp.letter = "[a-zA-Z]" 16 | regexp.tab = "\\t" 17 | regexp.space = "\\s" 18 | regexp.word = "\\w" 19 | regexp.digit = "\\d" 20 | regexp.newline = "\\n" 21 | regexp.return = "\\r" 22 | regexp.eol = "(?:(?:\\n)|(?:\\r\\n))" 23 | 24 | regexp.naming = {} 25 | 26 | regexp.escape = function (source){ 27 | if (source == null) return "" 28 | return String(source).replace( /([.*+?^=!:${}()|[\]\/\\])/g, "\\$1" ) 29 | } 30 | 31 | regexp.prototype.add = function(){ 32 | if(this.source == defaultSource){ 33 | this.source = "" 34 | } 35 | this.source += toArray(arguments).join("") 36 | return this 37 | } 38 | 39 | regexp.prototype.start = function(val){ 40 | return this.add( "^", regexp.escape(val) ) 41 | } 42 | 43 | /** 44 | * .must('a').atleast(3) 45 | */ 46 | regexp.prototype.atleast = function(times){ 47 | return this.add( "{", times, ",}" ) 48 | } 49 | 50 | /** 51 | * .must('a').has(1, 5) 52 | * .must('a').has(5) 53 | */ 54 | regexp.prototype.has = function(){ 55 | return this.add( "{", toArray(arguments).join(","), "}" ) 56 | } 57 | 58 | regexp.prototype.must = function(val){ 59 | return this.add( regexp.escape(val) ) 60 | } 61 | 62 | regexp.prototype.maybe = function(val){ 63 | return this.add( "(?:", regexp.escape(val), ")?" ) 64 | } 65 | 66 | regexp.prototype.something = function(){ 67 | return this.add( "(?:.)+" ) 68 | } 69 | 70 | regexp.prototype.anything = function(){ 71 | return this.add( "(?:.)*" ) 72 | } 73 | 74 | regexp.prototype.anythingBut = function(val){ 75 | return this.add( "(?:[^", val, "])*" ) 76 | } 77 | 78 | regexp.prototype.somethingBut = function(val){ 79 | return this.add( "(?:[^", val, "])+" ) 80 | } 81 | 82 | regexp.prototype.find = function(val){ 83 | return this.add( "(", val, ")" ) 84 | } 85 | 86 | /** 87 | * either("org", "com", "net") 88 | */ 89 | regexp.prototype.either = function(){ 90 | return this.add( "(?:", toArray(arguments).join("|"), ")" ) 91 | } 92 | 93 | regexp.prototype.end = function(val){ 94 | return this.add( regexp.escape(val), "$" ) 95 | } 96 | 97 | regexp.prototype.global = function(){ 98 | this.globalFlag = true 99 | return this 100 | } 101 | 102 | regexp.prototype.ignoreCase = function(){ 103 | this.ignoreCaseFlag = true 104 | return this 105 | } 106 | 107 | regexp.prototype.multiline = function(){ 108 | this.multilineFlag = true 109 | return this 110 | } 111 | 112 | regexp.prototype.toRegExp = function(name){ 113 | var flags = (this.multilineFlag? "m": "") + (this.ignoreCaseFlag? "i": "") + (this.globalFlag? "g": "") 114 | var re = new RegExp(this.source, flags) 115 | if(name) regexp.naming[name] = re 116 | return re 117 | } 118 | 119 | regexp.prototype.toString = function(){ 120 | return this.source 121 | } 122 | 123 | module.exports = regexp 124 | --------------------------------------------------------------------------------