JS regular expressions for fisrt one letter and numbers - javascript

I try make regular expression for one letter Z and 12 digits.
event.target.value = event.target.value.replace(/[^Z{1}+(\d{12})]/, '');
It is necessary for me that in the input field it was possible to enter one letter Z and then 12 digits only.
Please help me.

The regexp is
/^Z\d{12}$/
^ matches the beginning of the string
Z matches the initial Z. There's no need for {1} since patterns always match exactly one time unless quantified.
\d{12} matches exactly 12 digits
$ matches the end of the string
const regex = /^Z\d{12}$/;
console.log(regex.test('Z123456789012')); // true
console.log(regex.test('X123456789012')); // false - begins with wrong letter
console.log(regex.test('Z1234567890')); // false - <12 digits
console.log(regex.test('Z123456A89012')); // false - letter mixed into digits
console.log(regex.test('Z123456789012345')); // false - >12 digits

Related

Validating userName using Regex

The only numbers in the username have to be at the end. There can be zero or more of them at the end.
Username letters can be lowercase and uppercase.
Usernames have to be at least two characters long. A two-letter username can only use alphabet letter characters.
I'm trying with this but I'm stalled. /\d+$\w+/gi
/^[a-z]{2,}\d*$/i is:
^ : the begining
[a-z] : a character (a to z), you can add as many allowed characters as you want
{2,} : at least 2 of them
\d* : 0 or more digits
$ : the end
i : ignore case sensetivity (both lowercases and uppercases are allowed)
Username having characters and digit and min 2 character long
/^[a-zA-Z]{2,}\d*$/i
Test result :
UserNam9 = pass
9username = fail
Userna99 = pass
usernameeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee = pass
Us = pass
U = fail
/^[A-z]{2,}[A-z0-9]{0,}$/
/^ // start of line
[A-z]{2,} //alphabet characters 2 or more
[A-z0-9]{0,} //numbers and alphabet
$/ // end of line
You've missed cases when there's a letter in the start, followed by 2 or more numbers.
U99 = fail
d345 = fail
My solution passes these tests, as well:
/^[a-z]{2,}\d*$|(?=\w{3,})^[a-z]{1,}\d+$/i
Using positive lookahead I am making sure that in the second case there are at least 3 alphanumeric characters.
Simplified version of /^[a-z]{2,}\d*$|(?=\w{3,})^[a-z]{1,}\d+$/i:
/^\D(\d{2,}|\D+)\d*$/i
Code explanation:
^ - start of input
\D - first character is a letter
\d{2,} - ends with two or more numbers
| - or
\D+ - has one or more letters next
\d* - and ends with zero or more numbers
$ - end of input
i - ignore case of input
This is my answer, it passed all the tests:
/^[a-z][a-z]+\d*$|^[a-z]\d{2,}$/i
First Part: 2 letters (or more) and zero or more numbers
Or
Second Part: 1 letter and 2 or more numbers

Match a repeating digit (the same one) exactly two times in Javascript with RegEx

I'm currently trying to match a repeating digit and so far I've got this:
pattern = /(\d){2}/
But when I test this pattern with a number of any length >= 2 it will return true. What I want to find is the following:
When I test the number 12344 it should return true and if the number is 12345 it should return false. But having a number of 12444 should also return false. I want to find the same digit repeated exactly twice.
EDIT: Thanks to anybody proposing a solution!
For this kind of task you have to use lookarounds and backreferences:
(?:^|(.)(?!\1))(\d)\2(?!\2)
Explanation:
(?: // match either...
^ // start of the string
| // or...
(.) // any character
(?!\1) // not followed by the exact same character
)
(\d) // then, match and capture a digit
\2 // and the same digit a 2nd time
(?!\2) // and assert the digit doesn't show up a 3rd time
/(00|11|22|33|44|55|66|77|88|99)/

Regular Expression only digits and blank spaces

I have a text input and I require that it only accepts digits (0-9) and blank spaces (" ").
The current regexp to validate this input is:
/((\d{1,})(\s{1,})?){1,}/
Which stands for: one or more groups base on a first group of one or more digits and an optional second group of one or more blank spaces
That will only let me introduce values as: 999999 (only digits) or " " (only blank spaces) or 91 08 8510 903 (mix of digits and spaces).
But actually, I also can insert aaa or other characters.
Dissection
Your regular expression doesn't accept only letters :
/((\d{1,})(\s{1,})?){1,}/.test('aaa') // false
Actually, any character is accepted if the input contains at least one digit :
/((\d{1,})(\s{1,})?){1,}/.test('a1a') // true
That being said, let's skim the fat from your pattern :
"{1,}" equals "+" -> ((\d+)(\s+)?)+
"(.+)?" equals ".*" -> ((\d+)\s*)+
useless brackets -> (\d+\s*)+
This result can be translated to : "one or more digits (\d+) followed by zero or more blank spaces (\s*), one or more times (()+), anywhere in the input". Alternatively, we could say : "at least one digit, anywhere in the input".
What you need is to replace "anywhere in the input" with "from the beginning to the end of the input". This is allowed by the following special characters : ^ (beginning of input) and $ (end of input). Let's make a bunch of tests to see how they work :
requirement regex input .test()
---------------------------------------------------------------------
must contain at least one digit /\d+/ 'a1a' true
must start with at least one digit /^\d+/ '1a' true
must start with at least one digit /^\d+/ 'a1' false
must end with at least one digit /\d+$/ '1a' false
must end with at least one digit /\d+$/ 'a1' true
only digits from the beginning to the end /^\d+$/ '1a1' false
Suggestion
Only digits potentially separated by one whitespace : /^\d+( \d+)*$/.
^ beginning of the input
\d+ a digit, one or more times
( \d+)* a whitespace + same as above, zero or more times
$ end of the input
Usage example :
var r = /^\d+( \d+)*$/;
var isValid = r.test(' 1 '); // false
var isValid = r.test('1 1'); // true
var isValid = r.test('1 1'); // false
More about regular expressions : http://www.javascriptkit.com/javatutors/redev.shtml.
try this one
$pattern = '/^[0-9 ]+$/';
if ( preg_match ($pattern, $text) )
{
echo 'allowed';
}
Try this regular expression
/(\d+\s*\d*)+$/
Visualize the results here http://regex101.com/r/dE5uR8
I have tested it online using
http://regexpal.com/
The above regular expression will not accept empty blank spaces at the start. You need atleast one digit. If you want to match the empty spaces at the start also change it to (\d*\s*\d*)+$ which will accept empty spaces also

Possible to make regular expression with sub-query

I am trying to write a regular expression for a bit of javascript code that takes a user's input of a mobile number and in one regular expression, performs the following checks:
Starts with 07
Contains only numbers, whitespace or dashes
Contains exactly 11 numbers
Is this possible to do in just one regular expression and if so, how please?
I don't think it is possible with one regex, but it is possible by testing for two conditions:
if(/^07[\d\- ]+$/.test(str) && str.replace(/[^\d]/g, "").length === 11) {
//string matches conditions
}
Explanation of the regex:
^: Anchor that means "match start of string".
07: Match the string 07. Together with the above, it means that the string must start with 07.
[: Beginning of a character class i.e., a set of characters that we want to allow
\d: Match a digit (equivalent to 0-9).
\-:
" ": Match whitespace (markdown doesn't let me show a single space as code)
]: End of character class.
+: One or more of the previous.
$: Anchor that means "match end of string". Together with the ^, this basically means that this regex must apply to the entire string.
So here we check to see that the string matches the general format (starts with 07 and contains only digits, dashes or spaces) and we also make sure that we have 11 numbers in total inside the string. We do this by getting of anything that is not a digit and then checking to see that the length of the string is equal to 11.
Since #Vivin throws out the challenge :
/^07([-\s]*\d){9}[-\s]*$/
^07 : begin with digits 07
( : start group
[-\s]* : any number of - or whitespace
\d : exactly one digit
){9} : exactly 9 copies of this group (11 digits including 07)
[-\s]* : optional trailing spaces or -
$ : end of string
Of course a more useful way might be as follows
if ((telNo = telNo.replace (/[-\s]+/g, '')).match (/^07\d{9}$/)) {
....
}
which has the advantage (?) of leaving just the digits in telNo
Thank you all for trying, but after a good while trying different ideas, I finally found a working "single" regular expression:
07((?:\s|-)*\d(?:\s|-)*){9}
This make sure that it starts with 07, only contains digits, whitespace or dashes, and only 11 of them (9 plus the first 2) are numbers.
Sorry to have wasted your time.
Explanation:
() - include in capture
(?:) - do not include in capture
\s - whitespace
| - or
- - dash
* - zero or more
\d - digits only
{9} - exactly nine of what is captured

Regex split string of numbers at finding of Alpha Characters

OK Regex is one of the most confusing things to me. I'm trying to do this in Javascript. I have a search field that the user will enter a series of characters. Codes are either:
999MC111
or just
999MC
There is ALWAYS 2 Alpha characters. BUT there may be 1-4 characters at the front and sometimes 1-4 characters at the end.
If the code ENDS with the Alpha characters, then I run a certain ajax script. If there are Numbers + 2 letters + numbers....it runs a different ajax script.
My struggle is I know \d is for 2 digits....but it may not always be 2 digits.
So what would my regex code be to split this into an array. or something.
I think correct regex would be (/^([0-9]+)([a-zA-z]+)([0-9]+)$/
But how do i make sure its ONLY 2 alpha characters in middle?
Thanks
You could use the regex /\d$/ to determine if it ends with a decimal.
\d matches a decimal character, and $ matches the end of the string. The / characters enclose the expression.
Try running this in your javascript console, line by line.
var values = ['999MC111', '999MC', '999XYZ111']; // some test values
// does it end in digits?
!!values[0].match(/\d$/); // evaluates to true
!!values[1].match(/\d$/); // evaluates to false
To specify the exact number of tokens you must use brackets {}, so if you know that there are 2 alphabetic tokens you put {2}, if you know that there could be 0-4 digits you put {0,4}
^([0-9]{0,4})([a-zA-z]{2})([0-9]{0,4})$
The above RegEx evaluates as follows:
999MC ---> TRUE
999MC111 --> TRUE
999MAC111 ---> FALSE
MC ---> TRUE
The splitting of the expression into capturing groups is done by means of grouping subexpressions into parentheses
As you can see in the following link:
http://regexr.com?2vfhv
you obtain this:
3 capturing groups:
group 1: ([0-9]{0,4})
group 2: ([a-zA-z]{2})
group 3: ([0-9]{0,4})
The regex /^\d{1,4}[a-zA-Z]{2}\d{0,4}$/ matches a series of 1-4 digits, followed by a series of 2 alpha characters, followed by another series of 0-4 digits.
This regex: /^\d{1,4}[a-zA-Z]{2}$/ matches a series of 1-4 digits, followed only by 2 alpha characters.
Ok so I didnt really care about the middle 2 characters....all that really mattered was the 1st set of numbers and last set of numbers (if any).
So essentially I just needed to deal with digits. So I did this:
var lead = '123mc444'; //For example purposes
var regex = /(\d+)/g;
var result = (lead.match(regex));
var memID = result[0]; //First set of numbers is member id
if(result[1] != undefined) {
var leadID = result[1];
}

Categories

Resources