JavaScript Regular Expression to match digits too - javascript

I use this regex /^[-.a-zA-Z\s]+$/ to match any string contains only English letters, dashes and dots.
I would like to modify it to make it match any digit too.
so all these strings will be accepted:
first
first-floor
1st floor
floor No. 1
how can I do this ?

Just add digits to your character class:
/^[-.a-zA-Z\d\s]+$/

you can also write:
/^[-.a-zA-Z0-9\s]+$/

Related

RegEx for single asterisk (*) and alphanumeric

I need a javascript regex that accepts any alphanumeric character (can be any amount of characters or 0 characters if an asterisk is present) and a single asterisk anywhere in the string (but it does not need the asterisk).
Matches
*
abc
*abc
abc*
a*bc
Invalid Matches
**
*_abc
*abc*
abc**
**abc
I have
^([A-Za-z\d*]?)+$
but that matches multiple asterisks and I'm not sure how to only allow one https://regex101.com/r/a1C9bf/1
You may use this regex with a negative lookahead:
/^(?!(?:.*\*){2})[A-Za-z\d*]+$/gm
Updated RegEx Demo
Negative lookahead (?!(?:.*\*){2}) fails the match if there are more than one * in input.
Without requiring any look-ahead, you could use ^([\da-zA-Z]+|[\da-zA-Z]*\*[\da-zA-Z]*)$
https://regex101.com/r/xW2IvR/2
You could do:
^(?=.)[A-Za-z\d]*\*?[A-Za-z\d]*$
This will match any string that that's at least one character long ((?=.)), starts with zero or more alphanumeric characters, contains an optional *, and ends with zero or more alphanumeric characters.
You could also replace [A-Za-z\d] with [^\W_] to make it a little shorter (but slightly harder to read):
^(?=.)[^\W_]*\*?[^\W_]*$
You want one match one of two possible cases:
an asterisk surrounded by zero or more alphanumeric characters
one or more alphanumeric characters
Then this is your regex:
^([a-zA-Z\d]*\*[a-zA-Z\d]*|[a-zA-Z\d]+)$

writing regular expression with constraints

I am working on a JavaScript regular expression for the following condition check.
Consignment number validation details:
Cnote length :12 Varchar
First Character should be Character Upper Case
Fifth Character may be character or integer
Remaining all integer
Examples of valid strings:
C991S1234567
C30811234567
I have no idea. I have tried a simple regular expression like checking only numbers or alphabets.
I have tried something like this:
^[0-9]
It allows only integers. I do not know how to add constraints to it. Any help will be greatly appreciated.
The regex for this task relies on character classes ([...]) and limiting quantifiers ({n}). ^[0-9] only checks if the first character is a digit.
You can use the following regex:
^[A-Z][0-9]{3}[a-zA-Z0-9][0-9]{7}$
See demo
Explanation:
^ - Beginning of the string
[A-Z] - First is an uppercase English letter
[0-9]{3} - The 2nd, 3rd and 4th characters are digits
[a-zA-Z0-9] - Fifth character is either a letter or digit
[0-9]{7} - Following 7 characters are digits
$ - End of string.

Regex match being overwritten

I'm trying to sanitise a phone number using Regex.
I don't want any separating characters between digits and I don't want the local (0) part. Separators could be any non-digit character.
ie. the number could be:
+44 (00) 845 740 4404
+44-(00)-845-740-4404
+44-(00)-845-740=4404 (unlikely but could be a typo)
This matches the (0) part fine:
http://regex101.com/r/cB6hN4/3
But if I add |\D+ to match a non-digit character, it overwrites my first match:
http://regex101.com/r/cB6hN4/2
How do I keep both matches within in the one regex?
Instead of using |\D+ at the end try to use |[^()\d]+
The regex will be \((\d+)\)|[^()\d]+
DEMO
But take into account that the parenthesis could not be used as a separator as you can see in the demo
I think you want something like this,
\((\d+)\)|(?:(?!\(\d+\))\D)+
DEMO
(?:(?!\(\d+\))\D)+ matches one or more non-digit characters but not of (\d+)

Regex to match any currency in page

Would like to take a string filled with text and extract the prices from it. For example, here's what it should match:
$1,234.55
$90.99
$90
$100.30
$203
Regex help here would be amazing, thank you so much for you time! This will be used in either PHP or Javascript.
You could use the below regex to match all the price strings,
\$\d+(?:,\d+)*(?:\.\d+)?
DEMO
Explanation:
\$ Matches the literal $ symbol.
\d+ Matches one or more numbers.
(?:,\d+)* Matches a comma and the following digits zero or more times.
(?:\.\d+)? Matches a dot and the following digits. ? turns the whole match as optional one.
Tada:
\$[\d,]+(?:\.\d+)?
Here it is in practice
Try this one /^\$(\d{1,3}(?:,\d{3})*)(?:\.(\d{1,2}))?$/:
"$1,121,234.55".match(/^\$(\d{1,3}(?:,\d{3})*)(?:\.(\d{1,2}))?$/);
(JS code)
This should be work for following patterns:
$1
$2.21
$56,231
$12,212.12
$56,823,163.12
First group $1 would be the full dollars (e.g. without cents) and second group $2 would be cents.
Description in details:
\$ is dollar sign
(\d{1,3}(?:,\d{3})*) captures whole dollar value
\d{1,3} it's the first three digits
(?:,\d{3})* everything which goes after first three digits
(?:\.(\d{1,2}))? captures the cents, it's optional
(?:\. ignores the dot
(\d{1,2}) and captures cents
Good luck!

Regex to match card code input

How can I write a regex to match strings following these rules?
1 letter followed by 4 letters or numbers, then
5 letters or numbers, then
3 letters or numbers followed by a number and one of the following signs: ! & # ?
I need to allow input as a 15-character string or as 3 groups of 5 chars separated by one space.
I'm implementing this in JavaScript.
I'm not going to write out the whole regex for you since this is homework, but here are some hints which should help you out:
Use character classes. [A-Z] matches all uppercase. [a-z] matches all lowercase. [0-9] matches numbers. You can combine them like so [A-Za-z0-9].
Use quantifiers like {n} so [A-Z]{3} gives you 3 uppercase letters.
You can put other characters in character classes. Let's say you wanted to match % or # or #, you could do [%##] which would match any of those characters.
Some meta-characters (characters which have special meaning in the context of regular expressions) will need to be escaped like so: \$ (since $ matches the end of a line)
^ and $ match the beginning and end of the line respectively.
\s matches white-space, but if you sanitize your input, you shouldn't need to use this.
Flags after the regex do special things. For example in /[a-z]/i, the i ignores case.
This should be it:
/^[a-z][a-z0-9]{4} ?[a-z0-9]{5} ?[a-z0-9]{3}[0-9][!&#?]$/i
Feel free to change 0-9 and [0-9] with \d if you see fit.
The regex is simple and readable enough. ^ and $ make sure this is a whole match, so there aren't extra characters before or after the code, and the /i flag allows upper or lower case letters.
I would start with a tutorial.
Pay attention to the quantifiers (like {N}) and character classes (like [a-zA-Z])
^[a-zA-Z][a-zA-Z0-9]{4} ?[a-zA-Z0-9]{5} ?[a-zA-Z0-9]{3}[\!\&\#\?]$

Categories

Resources