super-regexp
Version:
Javascript Regular Expressions with Super Powers
162 lines (112 loc) • 4.64 kB
JavaScript
!function(e){if("object"==typeof exports)module.exports=e();else if("function"==typeof define&&define.amd)define(e);else{var f;"undefined"!=typeof window?f=window:"undefined"!=typeof global?f=global:"undefined"!=typeof self&&(f=self),f.SuperRegExp=e()}}(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);throw new Error("Cannot find module '"+o+"'")}var f=n[o]={exports:{}};t[o][0].call(f.exports,function(e){var n=t[o][1][e];return s(n?n:e)},f,f.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(_dereq_,module,exports){
//Apply polyfill for IE8 and IE9
if (!Array.prototype.forEach) {
_dereq_('./polyfill');
}
function SuperRegularExpression(description) {
var that = {};
var flags, regexp_c,
patterns = [];
function compile_reg_exp(patterns) {
var reg_exp_str = '';
patterns.forEach(function (e, i, patterns) {
reg_exp_str += e.pattern;
});
return new RegExp(reg_exp_str, flags);
};
that.pat = function (pattern, comment) {
patterns.push({
pattern: pattern,
comment: comment
});
return that;
};
that.flags = function (flags_parammeter) {
flags = flags_parammeter;
return that;
};
that.exec = function (string) {
regexp_c = regexp_c || compile_reg_exp(patterns);
return regexp_c.exec(string);
};
/*
Accumulate each indivual pattern one at a time and show the results.
Example:
pat_A.exec(string);
(pat_A + pat_B).exec(string);
(pat_A + pat_B + pat_C).exec(string);
...
*/
that.debug = function (string) {
var i, regexp_c, sliced_patterns,
debug_result = [],
len = patterns.length;
for (i = 0; i < len; i++){
sliced_patterns = patterns.slice(0, i + 1);
regexp_c = compile_reg_exp( sliced_patterns );
debug_result.push({
patterns: sliced_patterns,
result: regexp_c.exec(string)
});
}
return debug_result;
};
that.docs = function () {
return {
description: description,
patterns: patterns
}
};
return that;
};
module.exports = SuperRegularExpression;
},{"./polyfill":2}],2:[function(_dereq_,module,exports){
//Polyfill taken from https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach
// Production steps of ECMA-262, Edition 5, 15.4.4.18
// Reference: http://es5.github.com/#x15.4.4.18
if (!Array.prototype.forEach) {
Array.prototype.forEach = function (callback, thisArg) {
var T, k;
if (this == null) {
throw new TypeError(" this is null or not defined");
}
// 1. Let O be the result of calling ToObject passing the |this| value as the argument.
var O = Object(this);
// 2. Let lenValue be the result of calling the Get internal method of O with the argument "length".
// 3. Let len be ToUint32(lenValue).
var len = O.length >>> 0;
// 4. If IsCallable(callback) is false, throw a TypeError exception.
// See: http://es5.github.com/#x9.11
if (typeof callback !== "function") {
throw new TypeError(callback + " is not a function");
}
// 5. If thisArg was supplied, let T be thisArg; else let T be undefined.
if (thisArg) {
T = thisArg;
}
// 6. Let k be 0
k = 0;
// 7. Repeat, while k < len
while (k < len) {
var kValue;
// a. Let Pk be ToString(k).
// This is implicit for LHS operands of the in operator
// b. Let kPresent be the result of calling the HasProperty internal method of O with argument Pk.
// This step can be combined with c
// c. If kPresent is true, then
if (k in O) {
// i. Let kValue be the result of calling the Get internal method of O with argument Pk.
kValue = O[k];
// ii. Call the Call internal method of callback with T as the this value and
// argument list containing kValue, k, and O.
callback.call(T, kValue, k, O);
}
// d. Increase k by 1.
k++;
}
// 8. return undefined
};
}
},{}]},{},[1])
(1)
});