commonregex
Advanced tools
Comparing version 0.1.2 to 0.2.0
@@ -20,38 +20,7 @@ 'use strict'; | ||
} | ||
}, | ||
jshint: { | ||
options: { | ||
jshintrc: '.jshintrc', | ||
reporter: require('jshint-stylish') | ||
}, | ||
gruntfile: { | ||
src: 'Gruntfile.js' | ||
}, | ||
lib: { | ||
src: ['lib/**/*.js'] | ||
}, | ||
test: { | ||
src: ['test/**/*.js'] | ||
} | ||
}, | ||
watch: { | ||
gruntfile: { | ||
files: '<%= jshint.gruntfile.src %>', | ||
tasks: ['jshint:gruntfile'] | ||
}, | ||
lib: { | ||
files: '<%= jshint.lib.src %>', | ||
tasks: ['jshint:lib', 'mochacli'] | ||
}, | ||
test: { | ||
files: '<%= jshint.test.src %>', | ||
tasks: ['jshint:test', 'mochacli'] | ||
} | ||
} | ||
}); | ||
// Default task. | ||
grunt.registerTask('default', ['watch']); | ||
grunt.registerTask('test', ['mochacli']); | ||
}; |
@@ -1,143 +0,155 @@ | ||
(function() { | ||
var CommonRegex = function(_text) { | ||
this.text = _text || ''; | ||
var _ = this; | ||
(function(root, factory) { | ||
if (typeof define === 'function' && define.amd) { | ||
// AMD. Register as an anonymous module. | ||
define(factory); | ||
} else if (typeof exports === 'object') { | ||
// CommonJS | ||
module.exports = factory(); | ||
} else { | ||
// Browser globals | ||
root.CommonRegex = factory(); | ||
} | ||
}(this, function() { | ||
var CommonRegex = function(_text) { | ||
this.text = _text || ''; | ||
/** | ||
* TODO: | ||
* | ||
* Better regex for IPv6 | ||
* getNumbersBetweenRange | ||
*/ | ||
if (_text !== undefined) { | ||
this.dates = this.getDates(); | ||
this.times = this.getTimes(); | ||
this.phones = this.getPhones(); | ||
this.links = this.getLinks(); | ||
this.emails = this.getEmails(); | ||
this.IPv4 = this.getIPv4(); | ||
this.IPv6 = this.getIPv6(); | ||
this.hexColors = this.getHexColors(); | ||
this.acronyms = this.getAcronyms(); | ||
this.money = this.getMoney(); | ||
this.percentages = this.getPercentages(); | ||
this.creditCards = this.getCreditCards(); | ||
this.addresses = this.getAddresses(); | ||
} | ||
var opt = function(regex) { | ||
return '(?:' + regex + ')?'; | ||
} | ||
return this; | ||
}; | ||
, group = function(regex) { | ||
return '(?:' + regex + ')'; | ||
} | ||
CommonRegex.prototype = { | ||
, any = function(regexes) { | ||
return regexes.join('|'); | ||
} | ||
dateRegex: (function() { | ||
var opt = function opt(regex) { | ||
return '(?:' + regex + ')?'; | ||
}, | ||
, dateRegex = (function() { | ||
var monthRegex = '(?:jan\\.?|january|feb\\.?|february|mar\\.?|march|apr\\.?|april|may|jun\\.?|june|jul\\.?|july|aug\\.?|august|sep\\.?|september|oct\\.?|october|nov\\.?|november|dec\\.?|december)' | ||
, dayRegex = '[0-3]?\\d(?:st|nd|rd|th)?' | ||
, yearRegex = '\\d{4}'; | ||
group = function group(regex) { | ||
return '(?:' + regex + ')'; | ||
}, | ||
dateRegex = group(any([dayRegex + '\\s+(?:of\\s+)?' + monthRegex, monthRegex + '\\s+' + dayRegex])) + '(?:\\,)?\\s*' + opt(yearRegex) + '|[0-3]?\\d[-/][0-3]?\\d[-/]\\d{2,4}'; | ||
any = function any(regexes) { | ||
return regexes.join('|'); | ||
}; | ||
return new RegExp(dateRegex, 'gim'); | ||
}()) | ||
, timeRegex = /((0?[0-9]|1[0-2]):[0-5][0-9](am|pm)|([01]?[0-9]|2[0-3]):[0-5][0-9])/gim | ||
, phoneRegex = /(\d?[^\s\w]*(?:\(?\d{3}\)?\W*)?\d{3}\W*\d{4})/gim | ||
, linksRegex = /((?:https?:\/\/|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}\/)(?:[^\s()<>]+|\((?:[^\s()<>]+|(?:\([^\s()<>]+\)))*\))+(?:\((?:[^\s()<>]+|(?:\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:\'".,<>?\xab\xbb\u201c\u201d\u2018\u2019]))/gim | ||
, emailsRegex = /([a-z0-9!#$%&'*+\/=?\^_`{|}~\-]+@([a-z0-9]+\.)+([a-z0-9]+))/gim | ||
, IPv4Regex = /\b((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b/gm | ||
, IPv6Regex = /((([0-9A-Fa-f]{1,4}:){7}[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){6}:[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){5}:([0-9A-Fa-f]{1,4}:)?[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){4}:([0-9A-Fa-f]{1,4}:){0,2}[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){3}:([0-9A-Fa-f]{1,4}:){0,3}[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){2}:([0-9A-Fa-f]{1,4}:){0,4}[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){6}((\b((25[0-5])|(1\d{2})|(2[0-4]\d)|(\d{1,2}))\b)\.){3}(\b((25[0-5])|(1\d{2})|(2[0-4]\d)|(\d{1,2}))\b))|(([0-9A-Fa-f]{1,4}:){0,5}:((\b((25[0-5])|(1\d{2})|(2[0-4]\d)|(\d{1,2}))\b)\.){3}(\b((25[0-5])|(1\d{2})|(2[0-4]\d)|(\d{1,2}))\b))|(::([0-9A-Fa-f]{1,4}:){0,5}((\b((25[0-5])|(1\d{2})|(2[0-4]\d)|(\d{1,2}))\b)\.){3}(\b((25[0-5])|(1\d{2})|(2[0-4]\d)|(\d{1,2}))\b))|([0-9A-Fa-f]{1,4}::([0-9A-Fa-f]{1,4}:){0,5}[0-9A-Fa-f]{1,4})|(::([0-9A-Fa-f]{1,4}:){0,6}[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){1,7}:))\b/gim | ||
, hexColorsRegex = /#(?:[0-9a-fA-F]{3}){1,2}\b/gim | ||
, acronymsRegex = /\b(([A-Z]\.)+|([A-Z]){2,})/gm | ||
, moneyRegex = /((^|\b)US?)?\$\s?[0-9]{1,3}((,[0-9]{3})+|([0-9]{3})+)?(\.[0-9]{1,2})?\b/gm | ||
, percentageRegex = /(100(\.0+)?|[0-9]{1,2}(\.[0-9]+)?)%/gm | ||
, creditCardRegex = /((?:(?:\d{4}[- ]){3}\d{4}|\d{16}))(?![\d])/gm; | ||
var monthRegex = '(?:jan\\.?|january|feb\\.?|february|mar\\.?|march|apr\\.?|april|may|jun\\.?|june|jul\\.?|july|aug\\.?|august|sep\\.?|september|oct\\.?|october|nov\\.?|november|dec\\.?|december)', | ||
dayRegex = '[0-3]?\\d(?:st|nd|rd|th)?', | ||
yearRegex = '\\d{4}'; | ||
/** | ||
* Used to get all the matches of a regex from a string | ||
* @param {String} text Text to look for the matches | ||
* @param {Regexp} regex Regex to match the text | ||
* @return {Array} Array of matches | ||
*/ | ||
var getMatches = function(text, regex) { | ||
text = text || _.text; | ||
var matches = text.match(regex); | ||
if(matches === null) { | ||
return []; | ||
} | ||
return matches; | ||
}; | ||
dateRegex = group(any([dayRegex + '\\s+(?:of\\s+)?' + monthRegex, monthRegex + '\\s+' + dayRegex])) + '(?:\\,)?\\s*' + opt(yearRegex) + '|[0-3]?\\d[-/][0-3]?\\d[-/]\\d{2,4}'; | ||
this.getDates = function(_text) { | ||
return getMatches(_text, dateRegex); | ||
}; | ||
return new RegExp(dateRegex, 'gim'); | ||
}()), | ||
this.getTimes = function(_text) { | ||
return getMatches(_text, timeRegex); | ||
}; | ||
timeRegex: /((0?[0-9]|1[0-2]):[0-5][0-9](am|pm)|([01]?[0-9]|2[0-3]):[0-5][0-9])/gim, | ||
phoneRegex: /(\d?[^\s\w]*(?:\(?\d{3}\)?\W*)?\d{3}\W*\d{4})/gim, | ||
linksRegex: /((?:https?:\/\/|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}\/)(?:[^\s()<>]+|\((?:[^\s()<>]+|(?:\([^\s()<>]+\)))*\))+(?:\((?:[^\s()<>]+|(?:\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:\'".,<>?\xab\xbb\u201c\u201d\u2018\u2019]))/gim, | ||
emailsRegex: /([a-z0-9!#$%&'*+\/=?\^_`{|}~\-]+@([a-z0-9]+\.)+([a-z0-9]+))/gim, | ||
this.getPhones = function(_text) { | ||
return getMatches(_text, phoneRegex); | ||
}; | ||
IPv4Regex: /\b((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b/gm, | ||
IPv6Regex: /((([0-9A-Fa-f]{1,4}:){7}[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){6}:[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){5}:([0-9A-Fa-f]{1,4}:)?[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){4}:([0-9A-Fa-f]{1,4}:){0,2}[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){3}:([0-9A-Fa-f]{1,4}:){0,3}[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){2}:([0-9A-Fa-f]{1,4}:){0,4}[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){6}((\b((25[0-5])|(1\d{2})|(2[0-4]\d)|(\d{1,2}))\b)\.){3}(\b((25[0-5])|(1\d{2})|(2[0-4]\d)|(\d{1,2}))\b))|(([0-9A-Fa-f]{1,4}:){0,5}:((\b((25[0-5])|(1\d{2})|(2[0-4]\d)|(\d{1,2}))\b)\.){3}(\b((25[0-5])|(1\d{2})|(2[0-4]\d)|(\d{1,2}))\b))|(::([0-9A-Fa-f]{1,4}:){0,5}((\b((25[0-5])|(1\d{2})|(2[0-4]\d)|(\d{1,2}))\b)\.){3}(\b((25[0-5])|(1\d{2})|(2[0-4]\d)|(\d{1,2}))\b))|([0-9A-Fa-f]{1,4}::([0-9A-Fa-f]{1,4}:){0,5}[0-9A-Fa-f]{1,4})|(::([0-9A-Fa-f]{1,4}:){0,6}[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){1,7}:))\b/gim, | ||
hexColorsRegex: /#(?:[0-9a-fA-F]{3}){1,2}\b/gim, | ||
acronymsRegex: /\b(([A-Z]\.)+|([A-Z]){2,})/gm, | ||
moneyRegex: /((^|\b)US?)?\$\s?[0-9]{1,3}((,[0-9]{3})+|([0-9]{3})+)?(\.[0-9]{1,2})?\b/gm, | ||
percentageRegex: /(100(\.0+)?|[0-9]{1,2}(\.[0-9]+)?)%/gm, | ||
creditCardRegex: /((?:(?:\d{4}[- ]){3}\d{4}|\d{16}))(?![\d])/gm, | ||
this.getLinks = function(_text) { | ||
return getMatches(_text, linksRegex); | ||
}; | ||
addressRegex: /\d{1,4} [\w\s]{1,20}(?:(street|avenue|road|highway|square|traill|drive|court|parkway|boulevard)\b|(st|ave|rd|hwy|sq|trl|dr|ct|pkwy|blvd)\.(?=\b)?)/gim, | ||
this.getEmails = function(_text) { | ||
return getMatches(_text, emailsRegex); | ||
}; | ||
/** | ||
* Used to get all the matches of a regex from a string | ||
* @param {String} text Text to look for the matches | ||
* @param {Regexp} regex Regex to match the text | ||
* @return {Array} Array of matches | ||
*/ | ||
getMatches: function getMatches(text, regex) { | ||
text = text || this.text; | ||
var matches = text.match(regex); | ||
if (matches === null) { | ||
return []; | ||
} | ||
return matches; | ||
}, | ||
this.getIPv4 = function(_text) { | ||
return getMatches(_text, IPv4Regex); | ||
}; | ||
getDates: function getDates(_text) { | ||
return this.getMatches(_text, this.dateRegex); | ||
}, | ||
this.getIPv6 = function(_text) { | ||
return getMatches(_text, IPv6Regex); | ||
}; | ||
getTimes: function getTimes(_text) { | ||
return this.getMatches(_text, this.timeRegex); | ||
}, | ||
this.getHexColors = function(_text) { | ||
return getMatches(_text, hexColorsRegex); | ||
}; | ||
getPhones: function getPhones(_text) { | ||
return this.getMatches(_text, this.phoneRegex); | ||
}, | ||
this.getAcronyms = function(_text) { | ||
return getMatches(_text, acronymsRegex); | ||
}; | ||
getLinks: function getLinks(_text) { | ||
return this.getMatches(_text, this.linksRegex); | ||
}, | ||
this.getMoney = function(_text) { | ||
return getMatches(_text, moneyRegex); | ||
}; | ||
getEmails: function getEmails(_text) { | ||
return this.getMatches(_text, this.emailsRegex); | ||
}, | ||
this.getPercentages = function(_text) { | ||
return getMatches(_text, percentageRegex); | ||
}; | ||
getIPv4: function getIPv4(_text) { | ||
return this.getMatches(_text, this.IPv4Regex); | ||
}, | ||
this.getCreditCards = function(_text) { | ||
return getMatches(_text, creditCardRegex); | ||
}; | ||
getIPv6: function getIPv6(_text) { | ||
return this.getMatches(_text, this.IPv6Regex); | ||
}, | ||
if(_text !== undefined) { | ||
this.dates = this.getDates(); | ||
this.times = this.getTimes(); | ||
this.phones = this.getPhones(); | ||
this.links = this.getLinks(); | ||
this.emails = this.getEmails(); | ||
this.IPv4 = this.getIPv4(); | ||
this.IPv6 = this.getIPv6(); | ||
this.hexColors = this.getHexColors(); | ||
this.acronyms = this.getAcronyms(); | ||
this.money = this.getMoney(); | ||
this.percentages = this.getPercentages(); | ||
this.creditCards = this.getCreditCards(); | ||
getHexColors: function getHexColors(_text) { | ||
return this.getMatches(_text, this.hexColorsRegex); | ||
}, | ||
getAcronyms: function getAcronyms(_text) { | ||
return this.getMatches(_text, this.acronymsRegex); | ||
}, | ||
getMoney: function getMoney(_text) { | ||
return this.getMatches(_text, this.moneyRegex); | ||
}, | ||
getPercentages: function getPercentages(_text) { | ||
return this.getMatches(_text, this.percentageRegex); | ||
}, | ||
getCreditCards: function getCreditCards(_text) { | ||
return this.getMatches(_text, this.creditCardRegex); | ||
}, | ||
getAddresses: function getAddresses(_text) { | ||
return this.getMatches(_text, this.addressRegex); | ||
} | ||
} | ||
return this; | ||
}; | ||
return CommonRegex; | ||
//Export as CommonJS module | ||
if(typeof module !== 'undefined' && module.exports) { | ||
module.exports = CommonRegex; | ||
} | ||
//Export as RequireJS module | ||
else if (typeof window.define === 'function' && window.define.amd) { | ||
define(function() { | ||
return CommonRegex; | ||
}); | ||
} | ||
//Expose CommonRegex as a browser global | ||
else { | ||
window.CommonRegex = CommonRegex; | ||
} | ||
}()); | ||
})); |
{ | ||
"name": "commonregex", | ||
"description": "Find a lot of kinds of common information in a string.", | ||
"version": "0.1.2", | ||
"version": "0.2.0", | ||
"homepage": "https://github.com/talyssonoc/commonregexjs", | ||
@@ -30,17 +30,14 @@ "author": { | ||
}, | ||
"dependencies": { | ||
}, | ||
"dependencies": {}, | ||
"devDependencies": { | ||
"mocha": "~1.10.0", | ||
"chai": "~1.8.0", | ||
"supertest": "~0.8.1", | ||
"grunt-contrib-jshint": "~0.6.0", | ||
"grunt-contrib-watch": "~0.5.0", | ||
"grunt-mocha-cli": "~1.3.0", | ||
"load-grunt-tasks": "~0.2.0", | ||
"time-grunt": "~0.1.1", | ||
"jshint-stylish": "~0.1.3", | ||
"grunt-contrib-watch": "~0.5.0" | ||
"chai": "^1.9.1", | ||
"grunt-mocha-cli": "^1.10.0", | ||
"load-grunt-tasks": "^0.6.0", | ||
"mocha": "^1.21.4", | ||
"time-grunt": "^1.0.0" | ||
}, | ||
"keywords": ["regex", "regexp"] | ||
"keywords": [ | ||
"regex", | ||
"regexp" | ||
] | ||
} |
@@ -48,2 +48,3 @@ CommonRegexJS | ||
* `creditCards` or `getCreditCards([text])` | ||
* `addresses` or `getAddresses([text])` | ||
@@ -50,0 +51,0 @@ Examples |
@@ -76,2 +76,16 @@ /* | ||
}); | ||
it('addresses', function() { | ||
var text = 'checkout the new place at 101 main st., 504 parkwood drive, 3 elm boulevard, 500 elm street, 101 main straight'; | ||
var matches = [ | ||
'101 main st.', | ||
'504 parkwood drive', | ||
'3 elm boulevard', | ||
'500 elm street' | ||
]; | ||
expect(commonRegex.getAddresses(text)).to.eql(matches); | ||
}); | ||
}); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
5
88
15009
208
1