i am trying to validate a number exactly 15 digits with 5 digits group separated by "-" , ":" , "/", that should start with either 5 or 4 or 37, it also should not contain any characters.
It means
37344-37747-27744 - valid
40344-37747-27744 - valid
59344-37747-27744 - valid
37344:37747:27744 - valid
37344/37747/27744 - valid
87887-98734-83422 - Not valid
548aa:aa764:90887 - not valid
37759\29938\92093 - not valid
I was only able to go this far
\d{5}[-:\/]\d{5}[-:\/]\d{5}
Please help.
Try this one:
(((5|4)\d{4})|((37)\d{3}))[-:\/]\d{5}[-:\/]\d{5}
https://regex101.com/r/VvTq5P/1
Edit:
I would also add: \b at the beggining and at the end:
\b(((5|4)\d{4})|((37)\d{3}))[-:\/]\d{5}[-:\/]\d{5}\b
so nothing like:
12337344-37747-27744
can pass the test.
You could use look ahead (?=.) to first check if your string starts ^ with numbers 5|4|37 that is the requirement, here's full pattern:
^(?=5|4|37)\d{5}[-:\/]\d{5}[-:\/]\d{5}$
Demo here
If I understand correctly the regex is this:
((5|4)\d{4}|37\d{3})[-:\/]\d{5}[-:\/]\d{5}
Needs to be
4XXXX, 5XXXX or 37XXX
So I split it up to accept the 3 forms
((5|4)\d{4}|37\d{3})[-:\/]\d{5}[-:\/]\d{5}
(5|4)\d{4} - looks for a number that starts with either 5 or 4 and four digits afterward.
Then the or 37\d{3} looks for 37 and three digits afterward.
If the separators have to be the same:
^(?=37|[54])\d{5}([-:\/])\d{5}\1\d{5}$
Explanation
^ Start of string
(?=37|[54]) Positive lookahead, assert either 37 or 5 or 4 to the right
\d{5} Match 5 digits
([-:\/]) Capture group 1, match one of - : /
\d{5} Match 5 digits
\1 Backreference to match the same separator as in group 1
\d{5} Match 5 digits
$ End of string
See a regex101 demo.
const regex = /^(?=37|[54])\d{5}([-:\/])\d{5}\1\d{5}$/;
[
"37344-37747-27744",
"40344-37747-27744",
"59344-37747-27744",
"37344:37747:27744",
"37344/37747/27744",
"87887-98734-83422",
"548aa:aa764:90887",
"37759\\29938\\92093",
"37344-37747/27744",
"37344:37747-27744"
].forEach(s =>
console.log(`${s} --> ${regex.test(s)}`)
)
The pattern without a lookaround using an alternation:
^(?:37\d{3}|[54]\d{4})([-:\/])\d{5}\1\d{5}$
See a regex101 demo
const regex = /^(?:37\d{3}|[54]\d{4})([-:\/])\d{5}\1\d{5}$/;
[
"37344-37747-27744",
"40344-37747-27744",
"59344-37747-27744",
"37344:37747:27744",
"37344/37747/27744",
"87887-98734-83422",
"548aa:aa764:90887",
"37759\\29938\\92093",
"37344-37747/27744",
"37344:37747-27744"
].forEach(s =>
console.log(`${s} --> ${regex.test(s)}`)
)
Related
The first 3 characters needs to be:
Exactly either ABC or ACD or BCD
Then followed be a hyphen -
Then followed by either a 5 or 8
Then any 4 numbers
Examples:
ABC-56789 (True)
AAA-56789 (False)
I have tried this:
/^[^ABC$|^ACD$|^BCD$][*-][5|8][0-9]{4}$/
How about use this expression?
^(ABC|ACD|BCD)-[5|8]\d{4}$
[] means character set. So, [ABC] means any A or B or C, not ABC.
And ^ means negated in []. So, regex you used may not work fine.
If you want to group the tokens, you should use ().
You can also use \d (digit) instead of [0-9].
Use this regex:
const regex = /^(?:ABC|ACD|BCD)-[58][0-9]{4}$/;
[
'ABC-56789',
'AAA-56789'
].forEach(str => {
console.log(str, '==>', regex.test(str));
})
Output:
ABC-56789 ==> true
AAA-56789 ==> false
Explanation of regex:
^ -- anchor at beginning of string
(?:ABC|ACD|BCD) -- non-capture group with OR combinations
- -- literla dash
[58] -- a 5 or 8
[0-9]{4} -- four digits
$ -- anchor at end of string
Learn more about regex: https://twiki.org/cgi-bin/view/Codev/TWikiPresentation2018x10x14Regex
Use parentheses, not square brackets, to group alternation patterns:
^(ABC|ACD|BCD)-[58][0-9]{4}$
You have to change the regex to the following:
/^(ABC|ACD|BCD)-(5|8)[0-9]{4}$/
[] match single characters, but you want to match three characters in the beginning, so you have to use the () to create a capturing group.
Using a single alternation:
^(?:ABC|[AB]CD)-[58][0-9]{4}$
Explanation
^ Start of string
(?: Non capture group for the alternatives
ABC Match literally
| Or
[AB]CD Match either ACD or BCD
) Close the non capture group
- Match literally
[58] Match either 5 or 8`
[0-9]{4} Match 4 digits 0-9
$ End of string
See a regex101 demo
I'm writing a regex that should match the following predicate:
Combination of letters and numbers except number 1.
EG: TRS234, A2B3C4, 2A3B4C, 223GFG
I came up with this regex:
const regex = /^(?:[^1]+[a-z]|[a-z]+[^1])[a-z][^1]*$/i
It matches almost every case except for 2A3B4C, I've been doing lot of research but I'm not getting why it's not working for that particular case. Any help or suggestions to improve the regex will be greatly appreciated.
Thanks in advance!
Note that [^1] matches any char but 1, i.e. it also matches §, ł, etc. Also, [a-z][^1]* matches a letter followed with any 0+ chars other than 1, so the regex does not validate the expected string pattern.
You may use
const regex = /^(?:[02-9]+[a-z]|[a-z]+[02-9])[a-z02-9]*$/i
Or, a variation:
const regex = /^(?=[a-z02-9]*$)(?:\d+[a-z]|[a-z]+\d)[a-z\d]*$/i
See the regex demo and the regex graph:
Details
^ - start of string
(?:[02-9]+[a-z]|[a-z]+[02-9]) - either of the two:
[02-9]+[a-z] - 1 or more digits other than 1 followed with a letter
| - or
[a-z]+[02-9] - 1 or more letters followed with a digit other than 1
[a-z02-9]* - 0 or more letters or digits other than 1
$ - end of string.
I am facing difficulty to allow slash "/" with an existing regex
Below is an existing Regex which allows dot and numbers:
val.match(/^[0-9]+(\.[0-9]{1,2})?$/)
I changes it to...
val.match(/^[0-9]+([./][0-9\/]{1,2})?$/)
But this one won't allow the number like 1.5/384 where both dot/period and slash simultaneously.
Can someone help me with it?
You may add an optional non-capturing group after your main pattern part to match 1 or 0 occurrences of / followed with 1 or more digits:
/^\d+(?:\.\d{1,2})?(?:\/\d+)?$/
^^^^^^^^^^
See the regex demo
Details
^ - start of string
\d+ - 1 or more digits
(?:\.\d{1,2})? - an optional sequence of . and then 1 or 2 digits
(?:\/\d+)? - an optional sequence of / and then 1+ digits
$ - end of string.
If the number after / can be float in the same format as the first number:
/^\d+(?:\.\d{1,2})?(?:\/\d+(?:\.\d{1,2})?)?$/
^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^
This should do what you want :
^(\d+(?:\.\d{1,2})?\/?(?:\d+\.\d{1,2})?)$
See this Regex101.com
Edit : Corrected the fact that it didn't match 1 or 1.5
I have some dates that look like this:
20160517124945-0600
20160322134822.410-0500
20160322134822-0500
I used RegexMagic to find this regex:
(?:[0-9]+){14}\.?(?:[0-9]+){0,3}-(?:[0-9]+){4}
The problem is it also accepts things like this:
20160322134822-05800
or
20160323542352352352134822-0500
Apparently {} doesn't mean what I thought it did. How can I ensure I can only enter 14 digits before the - (or optional .) and 4 after?
Note that your regex did not function as expected because (?:[0-9]+){0,3} means match 1 or more digits zero, one, two or three times. That means, it matched any amount of digits.
It seems you need an optional group for the . followed with 1+ digits and you need to replace + with the limiting quantifiers:
^\d{14}(?:\.\d+)?-\d{4}$
See the regex demo.
Explanation:
^ - start of string (we need to ensure we only match 14 first digits before . or -)
\d{14} - 14 digits
(?:\.\d+)? - 1 or 0 sequences of . + 1 or more digits
- - a hyphen
\d{4} - 4 digits
$ - end of string
var re = /^\d{14}(?:\.\d+)?-\d{4}$/;
var strs = ['20160517124945-0600', '20160322134822.410-0500', '20160322134822-0500', '20160322134822-05800','20160323542352352352134822-0500'];
for (var s of strs) {
document.body.innerHTML += s + ": " + re.test(s) + "<br/>";
}
Try:
^(\d{14})(\.\d{0,3})?-(\d{4})$
Here's my version:
^[0-9]{14}(\.[0-9]{1,3})?-[0-9]{4}$
For some reason I always like using [0-9] instead of \d
So we're doing the start of the string with: ^
Then 14 numbers 0-9
Then an optional group starting with a period and then up to three numbers (I'm assuming a period and then no numbers after wouldn't be acceptable. This is in contrast to what you posted)
A hyphen
Four numbers
Then the end of the string with: $
I suppose you could simplify your regexp like this:
^(\d{14})(\.\d{3})?(-\d{4})$
test https://regex101.com/r/lF9gX0/1
I need a regex for validating a string / decimal in JavaScript.
Which can be max 9 elements long with 2 decimals
Simply
123 - valid
123456789 - valid
1234567896 - invalid ( max 10 chars )
123. - invalid
123.2 - valid
123.32 valid
123.324 invalid ( 3 decimal points )
So I wrote a regexp like this
/^([0-9]{1,9})+[.]+([0-9]{0,2})$/
Can any one plz fine tune this regex
You can use regex ^(?=.{0,10}$)\d{0,9}(\.\d{1,2})?$
$('input').on('input', function() {
$(this).css('color', this.value.match(/^(?=.{0,10}$)\d{0,9}(\.\d{1,2})?$/) ? 'green' : 'red');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<input type=text/>
Regex explanation here
Give the following a try:
^\d{1,9}(\.\d{1,2})?$
Something like this?
/^[0-9]{1,9}(\.[0-9]{0,2})?$/
You may use a negative lookahead at the beginning to apply a length restriction to the whole match:
^(?!\S{10})\d+(?:\.\d{1,2})?$
See regex demo
^ - start of string
(?!\S{10}) - no more than 9 non-whitespace characters from the beginning to end condition
\d+ - 1 or more digits
(?:\.\d{1,2})? - 1 or zero groups of . + 1 or w2 digits
$ - end of string
However, you might as well just match the float/integer numbers with ^\d+(?:\.\d{1,2})?$ and then check the length of the matched text to decide whether it is valid or not.
Note that in case you have to omit leading zeros, you need to get rid of them first:
s = s.replace(/^0+/, '');
And then use the regex above.