43 lines
999 B
JavaScript
43 lines
999 B
JavaScript
/*
|
|
---
|
|
name : sg-regex-tools
|
|
description : A few super-handy tools for messing around with RegExp
|
|
|
|
authors : Thomas Aylott
|
|
copyright : © 2010 Thomas Aylott
|
|
license : MIT
|
|
|
|
provides : [combineRegExp]
|
|
...
|
|
*/
|
|
;(function(exports){
|
|
|
|
exports.combineRegExp = function(regex, group){
|
|
if (regex.source) regex = [regex]
|
|
|
|
var names = [], i, source = '', this_source
|
|
|
|
for (i = 0; i < regex.length; ++i){ if (!regex[i]) continue
|
|
this_source = regex[i].source || ''+regex[i]
|
|
if (this_source == '|') source += '|'
|
|
else {
|
|
source += (group?'(':'') + this_source.replace(/\s/g,'') + (group?')':'')
|
|
if (group) names.push(group)
|
|
}
|
|
if (regex[i].names) names = names.concat(regex[i].names)
|
|
}
|
|
try {
|
|
regex = new RegExp(source,'gm')
|
|
}
|
|
catch (e){
|
|
throw new SyntaxError('Invalid Syntax: ' + source +'; '+ e)
|
|
}
|
|
// [key] → 1
|
|
for (i = -1; i < names.length; ++i) names[names[i]] = i + 1
|
|
// [1] → key
|
|
regex.names = names
|
|
return regex
|
|
}
|
|
|
|
}(typeof exports != 'undefined' ? exports : this))
|