I have an input box and the condition is to allow the user to enter only numbers, the numbers entered should be in the following format in groups of 4, ex: 4444 5555 and the maximum number of characters to be entered in the textbox should be 9. I am pretty new to regex, so have no clue of how to start. A working sample in fiddle would be of great help.
If the requirement is strictly 10 numbers in the above grouping with spaces in the middle, the regex is simple:
/^\d{4}\s\d{4}\s\d{2}$/
Where \d means that it would only match a numeric character, {4} means that it would look exactly 4 times for the previous match (\d), and in this case that would match 4 numeric characters. \s means one whitespace, and similarly like the {4}, \d{2} matches 2 numeric characters. The ^ and $ mean start of the string to be matched and end of the string to be matched respectively.
Hope this helps.
If the length is fixed then you can just use \d to represent a digit
/^\d\d\d\d \d\d\d\d \d\d\d\d \d\d$/
or use the {n} multiplier instead
/^\d{4} \d{4} \d{4} \d\d$/
if instead the total length is arbitrary and you just want to be sure that every four digits you have a space things are just slightly more complex:
/^(\d{4} )*\d{1,4}$/
the meaning is that you want zero or more groups formed with 4 digits and one space followed by 1 to 4 digits. In the last part you can use {0,4} if you also want to accept an empty string as a valid response.
If you want 1 or more of something use '+'. For example 4+ would be 1 or more consecutive '4's.
Use * to for things that you want 0 or more of!
Use parentheses for groups of characters or groups of other groups.
If you want a space in between, then use the space character between two of them.
It looks like you want 1 or more '4's followed by 0 or more (space followed by 1 or more '4's)
This regular express would match all of the following strings: "4+( 4+)*"
44444
4 44 4
4 4 4
4
4444444444
4 4
44444444444444 44444444444444444 4444444444444444
4444 4444 44
As per example provided this regex will help
/^[0-9][0-9 ]*$/
This represent numbers with spaces in between. For eg. 444 444. But if you put in this way ' 444 444' like first inserting space then start the numbers then it wont allow.
For that you can use /^[0-9 ]*$/
^ represent start and $ represent end. So between start and end you can write numbers with spaces.
Related
I have been working on a regex for validating an alphanumeric string with the rules as below:
The first FOUR starting characters must be numbers and last
TWO characters must be alphabets.
The space is OPTIONAL but must be placed between two characters,
meaning trailing space is not allowed.
The length of postal code must be 6 characters if SPACE is
not included and 7 characters if space is included.
Eg.
1111 ZZ
111 1ZZ
1 111ZZ
1111ZZ
I tried using ^[0-9]{4}[A-Za-z]{2}$|^(?=[\d|\D]+ [\d|\D]+).{7}$ but this also validates 9999 1A as TRUE which should actually be FALSE.
Any leads or help will be appreciated :)
(?=^.{6,7}$)^(([0-9] ?){4}( ?[a-zA-Z]){2})$
will match
1111 ZZ
111 1ZZ
1 111ZZ
1111ZZ
1111 ZZ
but not
9999 1A
11111 Z
1111111
11 11 ZZ
https://regex101.com/r/lByOx6/1
EDIT: explanation
The "Positive Lookahead" part:
(?=^.{6,7}$) this only matches if the string meets the requirements, BUT it does not 'consume' the characters.
. is any character
{6,7} is about repetitions
so (?=^.{6,7}$) is matched if the string has 6 or 7 characters, no matter what
Then the following part already 'consumes' the string to say that I want at the start 4 repetitions of numbers and optionally space, and at the end 2 repetitions of letters and optionally space. The second part would accept strings such as 1 1 1 1 Z Z but as those are more than 7 characters, the first part wouldn't let the string match.
I suggest simplifying the problem ahead of time, by reducing all white spaces, which you seem to be uninterested in anyway:
var candidate = input.replaceAll(/\s/mg, '');
Then the regex is simply: /^\d{4}[A-Za-z]{2}$/
If, however, you need to validate, that there actually are no leading or trailing white spaces, you can validate that ahead of time, and return a negative result right away.
Another option is to check if the string contains an optional space between the first and the last non whitespace character.
Then match the first digit followed by 3 digits separated by an optional space and 2 or 3 times a char a-zA-Z or a space.
Using a case insensitive match:
^(?=\S+ ?\S+$)\d(?: ?\d){3}[A-Z ]{2,3}$
Explanation
^ Start of string
(?= Positive lookahead, assert what on the right is
\S+ ?\S+$ Match optional space between the first and the last non whitespace char
) Close lookahead
\d(?: ?\d){3} Match a digit and repeat 3 times an optional space and a digit
[a-zA-Z ]{2,3} Match 2-3 times either a char a-zA-Z or a space
$ End of string
Regex demo
I have a numeric code which varies in length from 6-11 digits
which is separated by hyphen after each 3 digits
possible combinations
123-456
123-456-78
123-456-7890
So, here I am trying to convert the user entered code to this format even when entered with spaces and hyphens in the middle.
For Ex:
123 456-7 -> 123-456-7
123456 789 -> 123-456-789
123456 -> 123-456
Valid user input format is 3digits[space or hyphen]3digits[space or hyphen]0to5digits
I tried it like this
code.replace(/^(\d{3})[- ]?(\d{3})[- ]?(\d{0,5})$/,'$1-$2-$3');
But when there are only 6 digits there is a hyphen(-) at the end of the number which is not desired.
123-456-
Could anybody help me with this? Thank you.
The easiest way is probably to just do this with a second replace:
code.replace(/^(\d{3})[- ]?(\d{3})[- ]?(\d{0,4})$/,'$1-$2-$3')
.replace(/-$/, '');
This is chaining a second replace function, which says "replace a - at the end of the string with an empty string". Or in other words, "if the last character of the string is - then delete it.
I find this approach more intuitive than trying to fit this logic all into a replace command, but this is also possible:
code.replace(
/^(\d{3})[- ]?(\d{3})[- ]?(\d{0,4})$/,
'$1-$2' + ($3 == null ? '' : '-') + $3
)
I think it's less obvious at a glance what this code i doing, but the net result is the same. If there was no $3 matched (i.e. your sting only contained 6 digits), then do not include the final - in the replacement.
I believe this will do it for you - replace
^(\d{3})[ -]?()|(\d{3})[ -]?(\d{1,5})
with
$1$3-$2$4
It has two alternations.
^(\d{3})[ -]?() matches start of line and then captures the first group of three digits ($1), then optionally followed by a space or an hyphen. Finally it captures an empty group ($2).
(\d{3})[ -]?(\d{1,5}) matches, and captures ($3), three digits, optionally followed by a space or an hyphen. Then it matches and captures (($4)) the remaining 1-5 digits if they're present.
Since the global flag is set it will make one or two iterations for each sequence of digits. The first will match the first alternation, capturing the first three digits into group 1. Group 2 will be empty.
For the second iteration the first match have matched the first three digits, so this time the second alternation will match and capture the next three digits into group 3 and then the remaining into group 4.
Note! If there only are three digits left after the first match, none of the alternations will match, leaving the last three digits as is.
So at the first iteration group 1 are digits 123. group 2, 3 and 4 are empty. The second iteration group 1 and two are empty, group 3 are the digits 456 and group 4 are digit 7-11.
This gives the first replace $1 = 123- plus nothing, and the second 456-67....
There's no syntax checking in this though. It assumes the digits has been entered as you stated they would.
See it here at regex101.
I need to extract certain part of Javascript string. I was thinking to do it with regex, but couldn't come up with one which does it correctly.
String can have variable length & can contain all possible characters in all possible combinations.
What I need to extract from it, is 10 adjacent characters, that match one of next two possible combinations:
9 numbers & 1 letter "X" (capital letter "X", not X as variable letter!)
10 numbers
So, if input string is this: "[1X,!?X22;87654321X9]ddee", it should return only "87654321X9".
I hope I've explained it good enough. Thanks in advance!
This Regex will work:
\d{9}X|\d{8}X\d|\d{7}X\d{2}|\d{6}X\d{3}|\d{5}X\d{4}|\d{4}X\d{5}|\d{3}X\d{6}|\d{2}X\d{7}|\d{1}X\d{8}|\d{10}|X\d{9}
As described, It need to match 9 digits and any letter, and the letter can be at any position of the sequence.
\d{9}X # will match 9 digits and a letter in the end
\d{8}X\d # will match 8 digits a lettter then a digit again
...
\d{1}X\d{8} # will match 1 digits a lettter then 8 digits
\{10} # will match 10 digits
Edited to match only X
You can use this much simpler regex:
/(?!\d*X\d*X)[\dX]{10}/
RegEx Breakup:
(?!\d*X\d*X) # negative lookahead to fail the match if there are 2 X ahead
[\dX]{10} # match a digit or X 10 times
Since more than one X is not allowed due to use of negative lookahead, this regex will only allow either 10 digits or ekse 9 digits and a single X.
RegEx Demo
This regex has few advantages over the other answer:
Much simpler regex that is easier to read and maintain
Takes less than half steps to complete which can be substantial difference on larger text.
Given this string the symbol for four is 4 like in 34. _1234_. How can regex match the individual 4 and the 4 in 34 but not the one between the two _ characters in _1234_.
Your requirements are not very specific, so sorry if this will not work in practice for the actual application.
/(?:(?:^|\b)[^\_][a-zA-Z0-9]*)(4)(?:[a-zA-Z0-9\.]*[^\_](?:\b|$))/g
It checks every word that it does not begin and end with underscores for a 4.
(?<!_\d*)4(?!\d*_)
It makes sure the 4 is neither followed nor follows any count of digits and then an underscore.
Edit: I'm sorry, this won't work because JS does not support negative lookbehinds.
Look for _\d+_|(4), and check for a match with the (4) group. Since the alternatives in | are tried left-to-right, the (4) group will only match 4s that aren't in numbers surrounded by underscores
so I got an assesment due and one the regular expressions is a phone digit that has 10 numbers but it also allows dashes "-". However it can only contain 2 dashes and start with 0. eg. 0x-xxxx-xxxx where x can be any digit between 0-9.
So, so far I've come up with the following regexes:
^[0-0][0-9-]{1,11}$
^[0-0][0-9-]{11}$
In the first one it works but allows any length, if I put 5 numbers it goes through as long as the first one is 0. In the second one I can put 2 dashes followed by each other or just fill it with dashes and it goes through.
Thanks for helping guys!
Match every of the three parts, separated by the dashes, like this:
^0\d-\d{4}-\d{4}$
This matches 0x-xxxx-xxxx. Demo: https://regex101.com/r/nW7wL5/1
If you also want to match the number without the dashes, use
^0\d-?\d{4}-?\d{4}$
Demo: https://regex101.com/r/gY0mC3/1
\d is the same as [0-9] but it's shorter.