-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathcommonregex.js
115 lines (85 loc) · 4.37 KB
/
commonregex.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
(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() {
function CommonRegex(_text) {
this.text = _text || '';
if (_text !== undefined) {
// Use lazy evaluation
regexesNames.forEach(function(r) {
Object.defineProperty(this, r, {
get: function() {
var propertyName = '_' + r;
if(!this[propertyName]) {
this[propertyName] = this.getMatches(this.text, CommonRegex.regexes[r]);
}
return this[propertyName];
}
});
}.bind(this));
}
}
/**
* 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
*/
CommonRegex.prototype.getMatches = function getMatches(_text, regex) {
var text = _text || this.text;
var matches = text.match(regex);
return matches || [];
};
CommonRegex.regexes = {
dates: (function() {
function opt(regex) {
return '(?:' + regex + ')?';
}
function group(regex) {
return '(?:' + regex + ')';
}
function any(regexes) {
return regexes.join('|');
}
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)';
var dayRegex = '[0-3]?\\d(?:st|nd|rd|th)?';
var yearRegex = '\\d{4}';
var datesRegex = group(any([dayRegex + '\\s+(?:of\\s+)?' + monthRegex, monthRegex + '\\s+' + dayRegex])) + '(?:\\,)?\\s*' + opt(yearRegex) + '|[0-3]?\\d[-/][0-3]?\\d[-/]\\d{2,4}';
return new RegExp(datesRegex, 'gim');
}()),
times: /\b((0?[0-9]|1[0-2])(:[0-5][0-9])?(am|pm)|([01]?[0-9]|2[0-3]):[0-5][0-9])/gim,
phones: /(\d?[^\s\w]*(?:\(?\d{3}\)?\W*)?\d{3}\W*\d{4})/gim,
links: /((?: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,
emails: /([a-z0-9!#$%&'*+\/=?\^_`{|}~\-]+@([a-z0-9]+\.)+([a-z0-9]+))/gim,
IPv4: /\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,
IPv6: /((([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,
hexColors: /#(?:[0-9a-fA-F]{3}){1,2}\b/gim,
acronyms: /\b(([A-Z]\.)+|([A-Z]){2,})/gm,
money: /((^|\b)US?)?\$\s?[0-9]{1,3}((,[0-9]{3})+|([0-9]{3})+)?(\.[0-9]{1,2})?\b/gm,
percentages: /(100(\.0+)?|[0-9]{1,2}(\.[0-9]+)?)%/gm,
creditCards: /((?:(?:\d{4}[- ]){3}\d{4}|\d{16}))(?![\d])/gm,
addresses: /\d{1,4} [\w\s]{1,20}(?:(street|avenue|road|highway|square|trail|drive|court|parkway|boulevard|circle)\b|(st|ave|rd|hwy|sq|trl|dr|ct|pkwy|blvd|cir)\.(?=\b)?)/gim
};
var regexesNames = Object.keys(CommonRegex.regexes);
var capitalize = function capitalize(string) {
return string.charAt(0).toUpperCase() + string.slice(1);
};
// Add a method relative to each one of the regexes
regexesNames.forEach(function(r) {
CommonRegex.prototype['get' + capitalize(r)] = function(text) {
if(text) {
return this.getMatches(text, CommonRegex.regexes[r]);
}
return this[r];
};
});
return CommonRegex;
}));