How to write regular expression for this condition - javascript

I have to validate some vehicle number which comes in this format
XY-01-AA-1234
XY is two alphabets
01 is two digits number
AA is one or two alphabets that may or may not be present in the string
1234 is a number between 1 to 9999
If AA is not present then the followed hypen should not be present in the string
I have tried with this :
'[A-Z]{2}-[0-9]{2}-[A-Z-]{0,2}[0-9]{1,4}'
check_vehicle_no(reg_no,'[A-Z]{2}-[0-9]{2}-[A-Z-]{0,2}[0-9]{1,4}');
function check_vehicle_no(str,expp){
return str.match(expp);
}
But it accept vehicle number in this format XY-01--1234.
ie, the accepted format for vehicle numbers are XY-01-1234 or XY-01-A-1234 or XY-01-AA-1234.

You should be using this regex:
/^[A-Z]{2}-[0-9]{2}-(?:[A-Z]{1,2}-)?[0-9]{1,4}$/
RegEx Demo
(?:[A-Z]{1,2}-)? pattern makes 1 or 2 uppercase letters followed by a hyphen optional.
To use it in your function:
function check_vehicle_no(str, expp) {
return str.match(expp);
}
Then call it as:
var re = /^[A-Z]{2}-[0-9]{2}-(?:[A-Z]{1,2}-)?[0-9]{1,4}$/;
check_vehicle_no(reg_no, re);
Note use of /.../ instead of '...' for regex literal string.
PS: It might be better to call regex.test, if you just want to validate the input.

As per your conditions,
[A-Z]{2}-[0-9]{2}-([A-Z-]{0,2}.-)?[0-9]{1,4}
This could do that trick for checking vehicle numbers

Related

Regex validating string of numbers

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"))

Javascript Combine 2 regexes

I have a couple of regex which I am planning to combine.
So the first regex is as below (allows amounts with particular thousand and decimal separators)
"^-?(\\d+|\\d{1,3}(,\\d{3})*)?(\\.(\\d+)?)?$"
I have similar other regexes (based on different locales e.g. other one would have comma as the decimal separator)
So with the above regex, following are Valid/Invalid values
123.11 (Valid)
1'23 (Invalid)
With the second regex, I want that the string can contain a max of 13 digits (including before or after the decimal)
^[^\\d]*?(\\d|\\d[^\\d]+){0,13}$
With the above regex, following are Valid/Invalid values
1234567890123 (Valid - 13 digits)
12345678901234 (Invalid - 14 digits)
1234567890.123 (Valid as 13 digits...10.3)
1234567890.1234 (Invalid as 14 digits...10.4)
Is it possible to somehow consolidate the 2 regex?
However, I do not want to touch the first regex (have different combinations based on different locales). But it would be nice to somehow dynamically append the 2nd regex into the first one ?
So, I am flexible with the 2nd regex as that is not based on any locale, but is going to be the same always and mainly validates for max of 13 digits in the string.
I'll then validate my string using the consolidated regex.
You may keep the first pattern as is, and just prepend it with
(?=^\D*(?:\d\D*){0,13}$)
The (?=^\D*(?:\d\D*){0,13}$) pattern represents a positive lookahead that matches a location that is immediately followed with
^ - start of string
\D* - 0+ non-digits
(?:\d\D*){0,13} - 0 to 13 occurrences of a digit followed with a non-digit char
$ - end of string.
Full JavaScript regex definition:
var regex1 = "^-?(\\d+|\\d{1,3}(,\\d{3})*)?(\\.(\\d+)?)?$"; // Not to be touched
var consolidated_regex = "(?=^\\D*(?:\\d\\D*){0,13}$)" + regex1;
See full regex demo.
Details

How to check hasMatch against a regular expression

I want to validate a form number against an array of regular expressions.
For now, I have this:
static bool isValidPhoneNumber(String input) {
final RegExp regex = new RegExp(r'^\(\d\d\d\)\d\d\d\-\d\d\d\d\d$');
return regex.hasMatch(input);
}
The above works for a number in the format (734)637-78673.
But I want to match also for formats where the country code maybe 1 or 2 digits long
(1)498-5539867, (23)938-6738983
(\d{1,3})\d{3}-\d*
The regex above should match all your phone numbers with a country code of 1-3 digits. I don't know how long the last row of digits can be so I added "*". You can replace it with the amount you need.

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 to accept atleast one alphabet one numeric char and one special Character

I was trying to write a regex that accetps a string that has atleast 1 number 1 alphabet and 1 special character ,
/^[a-zA-Z][a-zA-Z][##$%^& .. and a bunch more]+$
But this is not working
You can use lookaheads:
/^(?=.*?[a-z])(?=.*?\d)(?=.*?[...])/i
[...] should hold the special characters you want.
var item = "1a$";
item.match(/^[0-9][a-zA-Z][^a-zA-Z0-9\s\t\n]$/)
That should work
You can use 3 little regex to accomplish that easily (it's more readable) :
[0-9]
[a-zA-Z]
[-_$#...]

Categories