This question already has an answer here:
Reference - What does this regex mean?
(1 answer)
Closed 2 years ago.
I have the following regex named 'pattern' that I am testing to try to restrict an input to only numbers. Why are both pattern.test("a") and pattern.test("1") returning true?
const pattern = /^[a-zA-Z0-9]*$/;
if (!pattern.test(event.target.value)) {
event.target.value = event.target.value.replace(/[^a-zA-Z0-9]/g, "");
}
Your expressions /^[a-zA-Z0-9]*$/ matches character ranges a-z, A-Z, 0-9. This will match any alphanumeric character. Adding the * means any number of times including zero. Meaning an empty string will also match.
If you want to match numeric and empty string only use /^[0-9]*$/.
If you want to match numeric only use /^[0-9]+$/.
Related
This question already has an answer here:
Reference - What does this regex mean?
(1 answer)
Closed 2 years ago.
I have a string in the following format:
ABC12233434343DEF
How can I extract only:
ABC12233434343
I want to leave out the ending set of characters of whatever length they might be.
There are several ways this is one:
.*?\d+
It will match anything at the beginning that is followed by numbers.
It may be also posible to limit the characters it can match initially, like if you was capital letters from A-Z, for example:
[A-Z]+\d+
Online Demo
This question already has answers here:
Negating a backreference in Regular Expressions
(6 answers)
Closed 2 years ago.
I'm trying to exclude strings where the first char is equal to the third char:
passing strings:
X9K3
V3Z5
not passing strings:
A4A9
R5R1
I tried ^(.).[^\1].$
but I'm too new to regex to understand why it failed.
You may use this regex:
^(.).(?!\1).+$
RegEx Demo
[^\1] does not do what it intends to because inside [...] every character becomes literal after first ^ so it just matches everything except \ and 1.
(?!\1) on the other hand is a negative lookahead which fails the match if character on next position is not same as what we captured in group #1.
This question already has answers here:
How to use double brackets in a regular expression?
(2 answers)
Difference between \w and \b regular expression meta characters
(5 answers)
Difference between * and + regex
(7 answers)
Closed 4 years ago.
How to capture any alphabet and any numeric into one group using regex.
I use basic regex to capture it, but every single character become a new match.
According to image below, I want the result like this:
match 1 = Q
match 2 = m
match 3 = C
match 4 = t2
match 5 = result (this word 'result' just an example, it can be replaced by any word)
Use a quantifier + for one or more
/[[:alnum:]]+/
See demo at regex101
Be aware that \n matches a newline and not a number. \d would be the short for digit.\w (word character) matches [A-Za-z0-9_]. It includes \d already .
This question already has answers here:
Match exact string
(3 answers)
Closed 4 years ago.
Hi this is my example 0xc1e5017fbc68aa3e56aa580708de9aa123d62d18
This is my reg ex ^0x[a-fA-F0-9]{1,40}. What I'm doing is 0x which is compulsory after that we can enter upto 40 alpha numeric digits. But not special characters. What should I do here so it will match correctly?
Your issue is that you need to assert where the string ends, or else you will match up until a special character is found, and get undesired matches.
So with your current regex and test string 0xc1!, 0xc1 would be matched, even though it is an invalid match.
How about using: ^0x[a-fA-F0-9]{1,40}$
This question already has answers here:
Regex for validating a string with pre-specified special characters
(3 answers)
Closed 8 years ago.
I have unsuccessfully be looking around the web for such a simple regex and can't seem to put it together.
I need a regex which allows any letter, numbers, whitespace and particular special characters only (# # $ & ( ) - _ /)
"Test #123 #Sample/test" is valid
"Test ^ £300" is not valid
Simply you could try the below regex,
^[\w\s##$&()\/-]+$
DEMO
^ Asserts that we are at the start.
[\w\s##$&()\/-]+ Matches one or more characters from the list.
$ Asserts that we are at the end.