Regex validating string of numbers - javascript

I am trying create regex witch will start with some number like 230, 420, 7456. Then could be some number in interval 1-9 but the final length must be 9.
For example 230888333 or 745623777
I create this:
([(230|420|7456)[0-9]{1,}]{9})
But it is not correct. Any idea how to make this correct?
Thaks.

The pattern that you tried is not anchored, and the current notation uses a character class [...] instead of a grouping (the ]{9} part at the end repeats 9 times a ] char)
If you use C# then use [0-9] to match a digit 0-9.
You can either assert 9 digits in total:
^(?=[0-9]{9}$)(?:230|420|7456)[0-9]+$
Regex demo
Or write an alternation for different leading lengths of digits:
^(?:(?:230|420)[0-9]{6}|7456[0-9]{5})$
Regex demo

You can simply add a check for length first and then simple regex
function checkString(str){
return str.length === 9 && /^(?:230|420|7456)\d+$/.test(str)
}
console.log(checkString("230888333"))
console.log(checkString("745623777"))
console.log(checkString("123"))
console.log(checkString("230888333123"))

Related

Regex limit the number of letters total in the entire string

I have this expression which is almost what I need:
/(?=^.{5,12}$)(^[a-zA-Z\u0590-\u05fe]{0,4}[0-9]{3,8}[a-zA-Z\u0590-\u05fe]{0,4}$)/
except, I need to only allow 4 letters total (4 in the beginning and 0 in the end,3 and 1, 2 and 2, 0 and 4, etc...)
allowed inputs:
11abcd11
1abcdefg123
abcd1234
unallowed inputs:
1abcd11
abcd123
1abcd12
is there a way to achieve this? thanks!
To make sure there are exactly four letters in the string, you can use
^(?=.{5,12}$)(?=(?:[^a-zA-Z\u0590-\u05fe]*[a-zA-Z\u0590-\u05fe]){4}[^a-zA-Z\u0590-\u05fe]*$)[a-zA-Z\u0590-\u05fe]{0,4}[0-9]{3,8}[a-zA-Z\u0590-\u05fe]{0,4}$
See the regex demo
The (?=(?:[^a-zA-Z\u0590-\u05fe]*[a-zA-Z\u0590-\u05fe]){4}[^a-zA-Z\u0590-\u05fe]*$) positive lookahead makes sure there are four sequences of any zero or more "non-letters" followed with a "letter" and then there are zero or more "non-letters" till the end of string.
If you can target ECMAScript 2018+ compliant engines you can use a Unicode-aware version:
/^(?=.{5,12}$)(?=(?:\P{L}*\p{L}){4}\P{L}*$)\p{L}{0,4}[0-9]{3,8}\p{L}{0,4}$/u
See this regex demo, where \p{L} matches letters. You may also replace it with \p{Alphabetic} which also matches letters that are Roman numbers.

Javascript question regarding the use of regex

I need to find a regex expression for a number 1 to 9, followed by trailing zeroes, followed by the end number 1 to 9. As like in a minesweeper game for clearing zeroes.
How do I match the part of an array where like i have 10009 or 2003 ? ,1to9 then zeroes, then 1to9?
How would I write it?
does this work?
updated: how do I ask this regex or another regex? the one i have below or a (trailing zeroes and 1-9)
(^[1-9][0+][1-9]$)
[1-9][0]+[1-9]
Move the + outside of the square brackets. Otherwise, it will match the literal character.
const regex = new RegExp("[1-9][0]+[1-9]")
function test(testCase){
console.log(regex.test(testCase))
}
test("10009")
test("2003")
To make the first digit optional, you can do:
[1-9]?[0]+[1-9]
const regex = new RegExp("[1-9]?[0]+[1-9]")
function test(testCase){
console.log(regex.test(testCase))
}
test("0009")
test("2003")
When you say [0+] then you are saying that select any of either 0 or +
you want is quantifier 0+ which means 0 one or more times
You can use ^[1-9]0+[1-9]$
const regex = /^[1-9]0+[1-9]$/;
function test(str) {
return regex.test(str);
}
console.log(test("10009"));
console.log(test("2003"));

How does the following code mean two consecutive numbers?

This is from an exercise on FCC beta and i can not understand how the following code means two consecutive numbers seeing how \D* means NOT 0 or more numbers and \d means number, so how does this accumulate to two numbers in a regexp?
let checkPass = /(?=\w{5,})(?=\D*\d)/;
This does not match two numbers. It doesn't really match anything except an empty string, as there is nothing preceding the lookup.
If you want to match two digits, you can do something like this:
(\d)(\d)
Or if you really want to do a positive lookup with the (?=\D*\d) section, you will have to do something like this:
\d(?=\D*\d)
This will match against the last digit which is followed by a bunch of non-digits and a single digit. A few examples (matched numbers highlighted):
2 hhebuehi3
^
245673
^^^^^
2v jugn45
^ ^
To also capture the second digit, you will have to put brackets around both numbers. Ie:
(\d)(?=\D*(\d))
Here it is in action.
In order to do what your original example wants, ie:
number
5+ \w characters
a non-number character
a number
... you will need to precede your original example with a \d character. This means that your lookups will actually match something which isn't just an empty string:
\d(?=\w{5,})(?=\D*\d)
IMPORTANT EDIT
After playing around a bit more with a JavaScript online console, I have worked out the problem with your original Regex.
This matches a string with 5 or more characters, including at least 1 number. This can match two numbers, but it can also match 1 number, 3 numbers, 12 numbers, etc. In order to match exactly two numbers in a string of 5-or-more characters, you should specify the number of digits you want in the second half of your lookup:
let regex = /(?=\w{5,})(?=\D*\d{2})/;
let string1 = "abcd2";
let regex1 = /(?=\w{5,})(?=\D*\d)/;
console.log("string 1 & regex 1: " + regex1.test(string1));
let regex2 = /(?=\w{5,})(?=\D*\d{2})/;
console.log("string 1 & regex 2: " + regex2.test(string1));
let string2 = "abcd23";
console.log("string 2 & regex 2: " + regex2.test(string2));
My original answer was about Regex in a vacuum and I glossed over the fact that you were using Regex in conjunction with JavaScript, which works a little differently when comparing Regex to a string. I still don't know why your original answer was supposed to match two numbers, but I hope this is a bit more helpful.
?= Positive lookahead
w{5,} matches any word character (equal to [a-zA-Z0-9_])
{5,}. matches between 5 and unlimited
\D* matches any character that\'s not a digit (equal to [^0-9])
* matches between zero and unlimited
\d matches a digit (equal to [0-9])
This expression is global - so tries to match all
You can always check your expression using regex101

regex : minimum total number of digits in a string

I'm trying to know how many digits there is in a string that is essentially like a password.
For now I have this regex :
^(?=.*[0-9]{3,})([a-zA-Z0-9_/+*.-]{6,})$
It works great when their is 3 digits in a row but not when they are separated in the whole string.
I need to be able to know if there is 3 digit in strings like those :
h123dasd
1hkh/23jd
1gvbn/*2fefse-
What can I do ?
You can use this regex:
/^(?=(?:\D*\d){3,})[a-zA-Z0-9_/+*.-]{6,}$/
This will enforce 3 digits in your input that may or may not be consecutive.
RegEx Demo
No need for such a complicated regex IMO - just extract digits from the string, concat the matches, and then check the length. Something like:
str.match(/\d+/g).reduce((p, c) => p + c).length > 3;
DEMO

Regex 3 number parts separated with dash

I want to create a RegExp validation in JavaScript for string which can have 3 parts:
P1-P2-P3
With following rules:
P1, P2 and P3 can only be digits
each part can be 1-13 digits long
entire string can not be longer then 20 characters
P2 and P3 parts are optional (meaning 34 is already a valid string, or 34-6565 or, 566-233455-23232).
Currently I have this, but I am missing the entire string length and I don't know how to define optional parts:
/^.\d{1,13}-\d{1,13}-\d{1,13}$/
Here are few valid entries: 5656, 33434-2323-45, 12-4345-12, 1234567890123-123456, 1234567890123-12-56
Invalid entries: 34453454351234566787, 1234567890123-1234567890123, 23455-233-123-3434, 34sd1322-23, 31234as, ...
You can use:
^(?=\d{1,13}(-\d{1,13}){0,2}$)[\d-]{1,20}$
Online Demo: http://regex101.com/r/sM6wQ7
Explanation:
Try this:
/(^\d{1,13}(-\d{1,13})?(-\d{1,13})?$){1,20}/
? means 0 or 1 of this, so this is useful for optional parts.
{1,20} at the end of the whole string to check it wont be longer than 20.
/^(?!.{21,})\d{1,13}(-\d{1,13}){0,2}$/
(?!.{21,}) at the beginning - negative lookahead meaning: not more than 20 characters.
The total string length check must be separated; the rest can be done with regular expressions:
var re = /^\d{1,13}(?:\-\d{1,13}){0,2}$/;
if (str.length <= 20 && str.match(re)) {
// valid
}
Try this one :
/^(?=.{0,20}$)\d{1,13}(-\d{1,13}){0,2}$/
Description :
^ beginning of the input
(?=.{0,20}$) followed by any char, 0 or 20 times, + end of the input
\d{1,13} a digit, 1 or 13 times
(-\d{1,13}){0,2} a dash + same as above, 0 or 2 times
$ end of the input
More on regular expressions : http://www.javascriptkit.com/javatutors/redev.shtml.
(\d{1,13}-){0,2}(\d{1,13})
Matches two sets of numbers of up to 13 digits of length followed by a dash, then a set of numbers of up to 13 digits of length. This approach is extensible, because you just need to change the number after the first capturing group to change the amount of number parts you want. I would recommend you checking via code if the string's length is appropriate before validating trough Regex. It will be faster and more readable.
Example: http://regex101.com/r/wI7uX1
You could also do this via Javascript, and it may be quite faster: http://jsfiddle.net/sauYY/

Categories