Regex to restrict certain numbers but allow all others - javascript

I want a regex to allow only numbers except certain numbers
example: Restrict 101 - 109. All others are allowed
Tried
var regex = new RegExp(/^([0-9]+|[^10[1-9]]| [^0])$/);
regex.test(101) // should give false
regex.test(109) // should give false
regex.test(0) // should give false
Any other value should give true
regex.test(100001) // should give true
This does not work

You can use a negative lookahead based regex to disallow certain numbers while matching all numbers:
/^(?!(0|10[1-9])$)\d+$/
RegEx Demo
(?!(0|10[1-9])$) is negative lookahead to disallow 0, and all numbers from 101-109.

Try this:
!/^0|10[1-9]$/.test(101)
function check_number(n){
return !/^0|10[1-9]$/.test(n);
}
document.write('101 : ' + check_number(101) + '<br>');
document.write('109 : ' + check_number(109) + '<br>');
document.write('0 : ' + check_number(0) + '<br>');
document.write('100001 : ' + check_number(100001) + '<br>');

Example: This restricts it to 238 - 971
This is just showing that its not the restriction that is hard,
its generating a number range regex.
A good tool that does it for you is here.
^(?!0*(?:23[8-9]|2[4-9]\d|[3-8]\d{2}|9[0-6]\d|97[0-1])$)\d+$
Expanded
^
(?!
0*
(?:
23 [8-9]
| 2 [4-9] \d
| [3-8] \d{2}
| 9 [0-6] \d
| 97 [0-1]
)
$
)
\d+
$

Related

How to match 2 separate numbers in Javascript

I have this regex that should match when there's two numbers in brackets
/(P|C\(\d+\,{0,1}\s*\d+\))/g
for example:
C(1, 2) or P(2 3) //expected to match
C(43) or C(43, ) // expect not to match
but it also matches the ones with only 1 number, how can i fix it?
You have a couple of issues. Firstly, your regex will match either P on its own or C followed by numbers in parentheses; you should replace P|C with [PC] (you could use (?:P|C) but [PC] is more performant, see this Q&A). Secondly, since your regex makes both the , and spaces optional, it can match 43 without an additional number (the 4 matches the first \d+ and the 3 the second \d+). You need to force the string to either include a , or at least one space between the numbers. You can do that with this regex:
[PC]\(\d+[ ,]\s*\d+\)
Demo on regex101
Try this regex
[PC]\(\d+(?:,| +) *\d+\)
Click for Demo
Explanation:
[PC]\( - matches either P( or C(
\d+ - matches 1+ digits
(?:,| +) - matches either a , or 1+ spaces
*\d+ - matches 0+ spaces followed by 1+ digits
\) - matches )
You can relax the separator between the numbers by allowing any combination of command and space by using \d[,\s]+\d. Test case:
const regex = /[PC]\(\d+[,\s]+\d+\)/g;
[
'C(1, 2) or P(2 3)',
'C(43) or C(43, )'
].forEach(str => {
let m = str.match(regex);
console.log(str + ' ==> ' + JSON.stringify(m));
});
Output:
C(1, 2) or P(2 3) ==> ["C(1, 2)","P(2 3)"]
C(43) or C(43, ) ==> null
Your regex should require the presence of at least one delimiting character between the numbers.
I suppose you want to get the numbers out of it separately, like in an array of numbers:
let tests = [
"C(1, 2)",
"P(2 3)",
"C(43)",
"C(43, )"
];
for (let test of tests) {
console.log(
test.match(/[PC]\((\d+)[,\s]+(\d+)\)/)?.slice(1)?.map(Number)
);
}

Regex to support toll free numbers along with UAE numbers

I have a regex that validates UAE numbers like: 00971585045336
here is the regex:
/00971(?:50|51|52|53|54|55|56|57|58|59|2|3|4|6|7|9)\d{7}$/
I have a requirement to add support for toll free numbers like:
0097180038249953 or 0097180022988
I am not good with regex so I need help to make it possible.
Thanks in advance.
You can use the following regex, assuming the tool free number format is 00971800, followed by 5 or 8 digits. Your original regex is simplified with character classes. The test shows 4 valid numbers, followed by invalid numbers:
const regex = /^00971((5\d|[234679])\d{7}|800(\d{5}|\d{8}))$/;
[
'00971581234567',
'0097171234567',
'0097180012345',
'0097180012345678',
'0097158123456',
'009715812345678',
'009717123456',
'00971712345678',
'00971800123456',
'009718001234567',
].forEach((str) => {
let valid = regex.test(str);
console.log(str + ' ==> ' + valid);
});
Output:
00971581234567 ==> true
0097171234567 ==> true
0097180012345 ==> true
0097180012345678 ==> true
0097158123456 ==> false
009715812345678 ==> false
009717123456 ==> false
00971712345678 ==> false
00971800123456 ==> false
009718001234567 ==> false
Explanation:
^ - start of string
00971 - expect literal text
( - start group, used for logical OR
( - start group, used for logical OR
5\d - expect a 5, followed by a digit
| - OR
[234679] - character class with a single char of allowed digits
) - end of group
\d{7} - 7 digits
| - OR
800 - literal text
( - start group, used for logical OR
\d{5} - 5 digits
| - OR
\d{8} - 8 digits
) - end of group
) - end of group
$ - end of string

JS Regex for full name and phone

can you help me with regex for :
full name -
can be in english or hebrew [\u0590-\u05FF]-this is the hebrew letter range.
need to be 2 or more words,
that every words contains at least one letter
(doesnt metter the upper or lower case)
Exmaples: Roei Grin, R G, roei grin, r G, roei gr iN,
"רועי גרין","רו ג", רועי גרי ן"
phone number-
must be 10 digits.
must start with 0
can have (not must) the "-" char, in the three or fourth place.
Exmaples: 0549129393, 058-9210348, 0266-391059
Here is a solution for both. Names and numbers have good examples, followed by bad examples:
const testNames = [
'Roei Grin',
'R G',
'roei grin',
'r G',
'roei gr iN',
'רועי גרי ן',
'רו ג',
'רועי גרין',
'Bad'
];
const testNumbers = [
'0549129393',
'058-9210348',
'0266-391059',
'1111',
'011111111',
'0999999-999',
'09999999999'
];
const nameRegex = /^[a-zA-Z\u0590-\u05FF]+( [a-zA-Z\u0590-\u05FF]+)+$/;
const numberRegex = /^0(\d{9}|\d{2}-\d{7}|\d{3}-\d{6})$/;
console.log('testNames:')
testNames.forEach(str => {
console.log('- "' + str + '" ==> ' + nameRegex.test(str));
});
console.log('testNumbers:')
testNumbers.forEach(str => {
console.log('- "' + str + '" ==> ' + numberRegex.test(str));
});
Output:
testNames:
- "Roei Grin" ==> true
- "R G" ==> true
- "roei grin" ==> true
- "r G" ==> true
- "roei gr iN" ==> true
- "רועי גרי ן" ==> true
- "רו ג" ==> true
- "רועי גרין" ==> true
- "Bad" ==> false
testNumbers:
- "0549129393" ==> true
- "058-9210348" ==> true
- "0266-391059" ==> true
- "1111" ==> false
- "011111111" ==> false
- "0999999-999" ==> false
- "09999999999" ==> false
Explanation of nameRegex:
^ ... $ - anchor at start and end of string
[a-zA-Z\u0590-\u05FF]+ - start with 1+ characters of alphabet and/or hebrew
( [a-zA-Z\u0590-\u05FF]+)+ - followed by 1+ pattern of: single space, followed by 1+ characters of alphabet and/or hebrew
Explanation of numberRegex:
^ ... $ - anchor at start and end of string
0 - start with 0
(\d{9}|\d{2}-\d{7}|\d{3}-\d{6}) - followed by either:
9 digits
or 2 digits, -, 7 digits
or 3 digits, -, 6 digits
You can use, this reg exp for your mask phone - ^[0]{1}(([\d]{2}[-]{0}[\d]{7})|([\d]{3}[-]{1}[\d]{6})|([\d]{2}[-]{1}[\d]{7}))

Regex: either only numbers or alphanumeric with '-' and '_'allowed but not number with '-' and '-'

I getting trouble pattern matching, I am trying to generate pattern which can accept either only numbers or alphanumeric with '-' and '_' allowed but not not number with '-' and '-' allowed and only '-' and '_' should not be allowed.
I tried the below one which somewhat working fine but fully working.
^[a-zA-Z0-9][a-zA-Z0-9-_]+$
I am trying to match cases like:
abcd = OK
as123 = Ok
as_as = Ok
as_12 = Ok
as-as = ok
12as = Ok
12_1as = Ok
123_12 = not Ok
12-12 = not Ok
1234 = ok
-- = not ok
__ = not ok
Thanks in advance
This does the job:
var test = [
'abcd',
'as123',
'as_as',
'as_12',
'as-as',
'12as',
'12_1as',
'123_12',
'12-12',
'1234',
'--',
'__',
];
console.log(test.map(function (a) {
return a+' : '+/^(?!\d+[-_]\d+$)[a-z0-9]+[-_]?[a-z0-9]+$/i.test(a);
}));
Explanation:
^ : begining of string
(?! : negative lookahead, make sure we don't have
\d+ : 1 or more digits
[-_] : - or _
\d+ : 1 or more digits
$ : end of string
) : end lookahead
[a-z0-]+ : 1 or more alphanumeric character
[-_]? : optional - or _
[a-z0-]+ : 1 or more alphanumeric character
$ : end of string
You are looking for such regex which benefits from a negative lookahead:
^(?!\d*(?:[-_]+\d*)*$)[\w-]+$
Live demo

RegExp: Look up non-formatted phone number with formatted number

If I have an E.164 formatted phone number and I want to look up any users with that phone number, though their number may not be formatted, what would that regex look like?
Example:
Given:
+1234567891
Regex should match any of:
(123) 456 7891
123-456-7891
+1234567891
123.456.7891
1234567891
Any of the above with trailing or leading whitespace.
var str = '+1234567891',
parts = str.match(/\+((\d{3})(\d{3})(\d{4}))/).slice(1),
num = parts.shift(),
rg = new RegExp(
'\\s*(?:\\+?' + [
num,
parts.join('.'),
parts.join('-'),
'\\(' + parts[0] + '\\) ' + parts.slice(1).join(' ')
].join('|') + ')\\s*');
In this case, it will produce
/\s*(?:\+?1234567891|123.456.7891|123-456-7891|\(123\) 456 7891)\s*/
Try:
/\s*(\+)?(\(\d{3}\)|\d{3})([ -.]|)\d{3}(\3)\d{4}\s*/
See the regex101 example
How it works
\s* matches any preceding white space
(\+)? optionally matches +
(\(\d{3}\)|\d{3}) matches (123) or just 123
([ -.]|) matches , -, . or nothing
\d{3} matches 123
(\3) whatever was matches by ( |-|.|)
\d{4} match 1234
\s* matches any following white space
I think the best you can do is normalize both numbers and compare the result. You can, for example, remove everything that is not a number ([^0-9]) and compare only the digits left.
You can use following regex :
^\s?(\(\d{3}\)|\+?\d{3})([\s-.])?\d{3}\2\d{4}\s?$
Demo :https://www.regex101.com/r/jM2fF4/6

Categories

Resources