이 작업은 한 두 번만하면됩니다. 다른 답변에서 제안한대로 케이스별로 처리하는 것이 좋습니다.
그러나 많은 작업을 수행해야하는 경우 몇 가지 도우미 기능을 사용하면 가독성이 향상 될 수 있습니다. 이 다소 더 만들고,
RegExp.any = function() {
var components = [],
arg;
for (var i = 0; i < arguments.length; i++) {
arg = arguments[i];
if (arg instanceof RegExp) {
components = components.concat(arg._components || arg.source);
}
}
var combined = new RegExp("(?:" + components.join(")|(?:") + ")");
combined._components = components; // For chained calls to "or" method
return combined;
};
RegExp.prototype.or = function() {
var args = Array.prototype.slice.call(arguments);
return RegExp.any.apply(null, [this].concat(args));
};
대안은 비 캡처 그룹에 싸여과 분리 연산자와 결합 : 예를 들면 : 당신이 그 기능을 사용하려면
var lower = /[a-z]/,
upper = /[A-Z]/,
digit = /[0-9]/;
// All of these are equivalent, and will evaluate to /(?:a-z)|(?:A-Z)|(?:0-9)/
var anum1 = RegExp.any(lower, upper, digit),
anum2 = lower.or(upper).or(digit),
anum3 = lower.or(upper, digit);
그리고 여기에 코드입니다 보다 복잡한 정규식에 대한 강력한 접근 방식.
도우미 기능을 호출하기 전에 앞에 코드를 포함해야합니다.
두 개의 별도 정규식을 사용하고 있거나'/ [a-zA-Z] /'를 원하십니까? –
나는 이것을 알고있다. 이것이 가능한지 알 필요가있다 –
가능한 복제본 [Regexp] (http://stackoverflow.com/questions/869809/combine-regexp) –