regex - Javascript - modify regexp constructor or add tokens -
i need parse input against complex regexp's have common elements.
i make "my text 2014".match(/{date}/);
where date long regexp or set of tokens like
regexp.tokens.date = /{year}|{month}|{day}/
etc.
and uses tokens
regexp.tokens.year = /** native regexp matching year number **/
i want {tokens}
(recursively) parsed native regexp before testing.
in other words - want add list of tokens , meaning , able make regexp using them.
i trying modify constructor of regexp dont make change.
rather hacking around existing stuff, should create own wrappers.
something like:
function myregex(src,flags) { for( var k in myregex.tokens) if( myregex.tokens.hasownproperty(k)) { src = src.replace(new regexp("\\{"+k+"\\}","g"),myregex.tokens[k]); } this.pattern = new regexp(src,flags); }; myregex.tokens = { "year": "your regex string here", // ... }; myregex.prototype.test = function(str) { return this.pattern.test(str); }; // define more methods needed
to use:
var regex = new myregex("{date}"); console.dir(regex.test("my text 2014"));
Comments
Post a Comment