When I was writing my linear-gradient() to -webkit-gradient() converter, I knew in advance that I would have to use a quite large regular expression to validate and parse the input. Such a regex would be incredibly hard to read and fix potential issues, so I tried to find a way to cut the process down in reusable parts.
Turns out JavaScript regular expression objects have a .source property that can be used in the RegExp constructor to create a new RegExp out of another one. So I wrote a new function that takes a string with identifiers for regexp replacements in {{ and }} and replaces them with the corresponding sub-regexps, taken from an object literal as a second argument:
/**
* Create complex regexps in an easy to read way
* @param str {String} Final regex with {{id}} for replacements
* @param replacements {Object} Object with the replacements
* @param flags {String} Just like the flags argument in the RegExp constructor
*/
RegExp.create = function(str, replacements, flags) {
for(var id in replacements) {
var replacement = replacements[id],
idRegExp = RegExp('{{' + id + '}}', 'gi');
if(replacement.source) {
replacement = replacement.source.replace(/^\^|\$$/g, '');
}
// Don't add extra parentheses if they already exist
str = str.replace(RegExp('\\(' + idRegExp.source + '\\)', 'gi'), '(' + replacement + ')');
str = str.replace(idRegExp, '(?:' + replacement + ')');
}
return RegExp(str, flags);
};
If you don’t like adding a function to the RegExp object, you can name it however you want. Here’s how I used it for my linear-gradient() parser:
self.regex = {};
self.regex.number = /^-?[0-9]*\.?[0-9]+$/;
self.regex.keyword = /^(?:top\s+|bottom\s+)?(?:right|left)|(?:right\s+|left\s+)?(?:top|bottom)$/;
self.regex.direction = RegExp.create('^(?:{{keyword}}|{{number}}deg|0)$', {
keyword: self.regex.keyword,
number: self.regex.number
});
self.regex.color = RegExp.create('(?:{{keyword}}|{{func}}|{{hex}})', {
keyword: /^(?:red|tan|grey|gray|lime|navy|blue|teal|aqua|cyan|gold|peru|pink|plum|snow|[a-z]{5,20})$/,
func: RegExp.create('^(?:rgb|hsl)a?\\((?:\\s*{{number}}%?\\s*,?\\s*){3,4}\\)$', {
number: self.regex.number
}),
hex: /^#(?:[0-9a-f]{1,2}){3}$/
});
self.regex.percentage = RegExp.create('^(?:{{number}}%|0)$', {
number: self.regex.number
});
self.regex.length = RegExp.create('{{number}}{{unit}}|0', {
number: self.regex.number,
unit: /%|px|mm|cm|in|em|rem|en|ex|ch|vm|vw|vh/
});
self.regex.colorStop = RegExp.create('{{color}}\\s*{{length}}?', {
color: self.regex.color,
length: self.regex.length
}, 'g');
self.regex.linearGradient = RegExp.create('^linear-gradient\\(\\s*(?:({{direction}})\\s*,)?\\s*({{colorstop}}\\s*(?:,\\s*{{colorstop}}\\s*)+)\\)$', {
direction: self.regex.direction,
colorStop: self.regex.colorStop
}, 'i');
(self in this case was a local variable, not the window object)
Pingback: Creating Grammatical Patterns Using XRegExp.build()
Pingback: JSSpy » Creating grammatical regexes using xregexp.build()
Pingback: XRegExp – Extended JavaScript Regular Expression Library()
Pingback: angara fahise()
Pingback: angara fahise()
Pingback: Jumpstart your Wealth Gene -http://jumpstartyourwealthgenereview.org/()
Pingback: winter vee opulence for life()
Pingback: du học nhật bản vừa học vừa làm()
Pingback: steroid suppliers()
Pingback: Term Life Insurance()
Pingback: car service()
Pingback: blog()
Pingback: steriod for sale()
Pingback: http://blogspot.com()
Pingback: hampton bay outdoor ceiling fans()
Pingback: game subway()
Pingback: sattamatka()
Pingback: Bank Tracker Bot()
Pingback: https://tribot.org/repository/script/id/138-ultimate-pest-control/()
Pingback: Watch Dragon Ball Super Episodes - Dragon Ball Super English/Dubbed()
Pingback: handmade furniture essex()
Pingback: Education()
Pingback: cutting cycle()
Pingback: read()
Pingback: stanozolol comprimido como tomar pastillas()
Pingback: xmt85c4wx5ctwxw3tcerthve56()
Pingback: alcoholism statistics()
Pingback: دانلود شهرزاد قسمت هفتم فصل دوم()
Pingback: دانلود شهرزاد قسمت نهم فصل دوم()
Pingback: دانلود شهرزاد قسمت هشتم فصل دوم()
Pingback: قیمت پنجره()
Pingback: dildos()
Pingback: دانلود قسمت دهم فصل دوم شهرزاد()
Pingback: دانلود قسمت ششم فصل دوم شهرزاد()
Pingback: دانلود قسمت 17 عاشقانه()
Pingback: Cherry Hilson()
Pingback: Allie Haze()
Pingback: Elsa Jean()
Pingback: Brittney White()
Pingback: hampton bay ceiling fan blog()
Pingback: terörist sosyalmarketim()
Pingback: Paleo Diet()
Pingback: April O'Neil()
Pingback: Free Stuff Worldwide()
Pingback: movie()
Pingback: bonus casinos()
Pingback: دانلود سریال عاشقانه()
Pingback: دانلود قسمت اول شهرزاد دوم()
Pingback: Loyola College for Sale()
Pingback: دانلود قسمت دوازدهم عاشقانه()
Pingback: دانلود قسمت پنجم شهرزاد دوم()
Pingback: دانلود قسمت سوم شهرزاد دوم()
Pingback: دانلود قسمت یازدهم شهرزاد دوم()
Pingback: دانلود قسمت اول فصل دوم شهرزاد()
Pingback: bypass windows 7 admin password()
Pingback: cheap nba jerseys()