gen = function(string, splitter){
return string
.split(splitter)
.map(function(s){return new RegExp(
"^" + s
.replace(/x/g, "\\d")
.replace(/(\+|\(|\))/g, "\\$1") + "$"
)})
}
string = "(xx) xxxxxxx\n+38 xxx xxxxxxx\n+38 (xxx) xxx xxx x\n+380 (xx) xxx-xxx-x\n+38 xxxxxxxxxx\n+38 xxx xxx xx xx\n+38 xxx xxx xxx x\n+380 (xx) xxx-xx-xx\n+380 (xx) xx-xx-xxx\n+38 xxx-xxx-xx-xx\n+38 (xxx)-xxx-xx-xx\n+38 (xxx)xx-xx-xxx\n+380xxxxxxxxxx"
regexes = gen(string, "\n")
strings = string.split(/\n/).map(function(s){return s.replace(/x/g, "1")})
with( console ){
log( regexes )
log(strings )
log( regexes.map(function(re, i){return re.test(strings[i])}) )
log( regexes.reverse().map(function(re, i){return re.test(strings[i])}))
}
/////////////////////////////////// OUT ///////////////////////////////////////
/*
[ /^\(\d\d\) \d\d\d\d\d\d\d$/,
/^\+38 \d\d\d \d\d\d\d\d\d\d$/,
/^\+38 \(\d\d\d\) \d\d\d \d\d\d \d$/,
/^\+380 \(\d\d\) \d\d\d-\d\d\d-\d$/,
/^\+38 \d\d\d\d\d\d\d\d\d\d$/,
/^\+38 \d\d\d \d\d\d \d\d \d\d$/,
/^\+38 \d\d\d \d\d\d \d\d\d \d$/,
/^\+380 \(\d\d\) \d\d\d-\d\d-\d\d$/,
/^\+380 \(\d\d\) \d\d-\d\d-\d\d\d$/,
/^\+38 \d\d\d-\d\d\d-\d\d-\d\d$/,
/^\+38 \(\d\d\d\)-\d\d\d-\d\d-\d\d$/,
/^\+38 \(\d\d\d\)\d\d-\d\d-\d\d\d$/,
/^\+380\d\d\d\d\d\d\d\d\d\d$/ ]
[ '(11) 1111111',
'+38 111 1111111',
'+38 (111) 111 111 1',
'+380 (11) 111-111-1',
'+38 1111111111',
'+38 111 111 11 11',
'+38 111 111 111 1',
'+380 (11) 111-11-11',
'+380 (11) 11-11-111',
'+38 111-111-11-11',
'+38 (111)-111-11-11',
'+38 (111)11-11-111',
'+3801111111111' ]
[ true,
true,
true,
true,
true,
true,
true,
true,
true,
true,
true,
true,
true ]
[ false,
false,
false,
false,
false,
false,
true,
false,
false,
false,
false,
false,
false ]
*/