Regex match being overwritten - javascript

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+)

Related

Why does my Regular Expression ignore the last character in each match

Below regex works properly except that it ignores the last character in each match.
\d{4}\b.*?(?=[^:]\d{4}(?! ml| kg)( [A-Za-z]{2}| \d{1}-| 1H-| [A-Za-z0-9],[A-Za-z0-9]| \D{1}-)|$)
My question is:
How can this be updated to also include the last character in each match
Below an example of the data:
https://regex101.com/r/XRlr4Q/1
The [^:] pattern in the lookahead requires a char other than : before the first four digits of a match.
You need to use a lookbehind (?<!:) there:
\d{4}\b.*?(?=(?<!:)\d{4}(?! ml| kg)(?: [A-Za-z]{2}| \d-| 1H-| [A-Za-z0-9],[A-Za-z0-9]| \D-)|$)
See the regex demo.
I rewrote a bit your Regex to match
4 digits
Followed by your special char and unlimited [A-a]
Then a positive lookahead to match everything until it sees the start sequence again or the end of file
I removed some things which you can add again if needed, but it works with your dataset.
\d{4}( [A-a]+).+?(?=\d{4}( [A-a]+) | $)
Here an example based on your DataSet :
https://regex101.com/r/HWYJEf/1

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]+)$

Regex JavaScript - how to check if a string is all letters and then all numbers

This would be ok: 'AAAAAAA1222222'
This would be not ok: '1AAAAA'
This would not be ok: 'AA1AA'
Just looking for a way to check if a string is ALL letters and then ONLY letters afterward.
This is an easy one.
^[A-Za-z]*[0-9]*$
That of course is assuming that no letters is OK.
For example, the above example would match
AAAAAAA
2222222
as well as an empty string.
If there must be at least one letter and at least one number, replace the * with +
^[A-Za-z]+[0-9]+$
text.match(/^[A-Z]*\d*$/i)
Read this as "start of string followed by any number of letters followed by any number of digits followed by the end of the string."
Note this will match "", "A", and "1". If you want there to be at least one letter and at least one number, use + instead of * in both spots.
Use a lookahead. Lookaheads are used for validation, I suggest you go through this.
Try this out: ^(?=[A-Za-z]*\d*$).+
DEMO
try this regex
\b\D+\d+\b
\b is the word boundary that won't allow for digits to come in the beginning and letters to come after the digits at the end.
I suggest something like this:
text.match(/\b\D+\d+[^\D]\b/);
The \b was suggested by Grace and is a good idea. Another option is to use the beginning and end anchors like:
text.match(/^\D+\d+[^\D]$/);
text.match(/^[a-zA-Z]+\d*$/);
Tests:
AAAAAAA1222222 - match
1AAAAA - no match
AA1AA - no match
AAAAAAA - match
2222222 - no match
If you dont want to match ALL Letters and at least one number change the quantifier of \d from +(1-infinite times) to *(0-infinity times)
more about regex quantifiers : link

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!

Validating phone number using regex with support for multuple numbers

I am not very experienced with regex and I need to validate phone numbers using javascript.
I have a textbox which need to be allowed to accept multiple phone numbers with a delimiter of ';' and the characters that can be allowed for the phone numbers are
Numbers
'+'
'-'
Could someone help me on how I can acheive this using javascript and regex/ regular expressions?
Example:
+91-9743574891;+1-570-456-2233;+66-12324576
I tried the following:
^[0-9-+;]+$
Am not sure if this is correct.
You have placed - in wrong place so, your regex is not working.
Try this(your RegEx, but slightly modified):
^[0-9+;-]+$
or
^[-0-9+;]+$
To include a hyphen within a character class then you must do one of the following:
escape the hyphen and use \-,
place hyphen either at the beginning or at the end of the character class.
As the hyphen is used for specifying a range of characters. So, regex engine understands [0-9-+;]+ match any of the characters between 0 to 9, 9 to +(all characters having decimal code-point 57[char 9] to 43[char +] and it fails) and ;.
To be a bit more restrictive, you could use the following regexp:
/^\+[0-9]+(-[0-9]+)+(;\+[0-9]+(-[0-9]+)+)*$/
What it will match:
+91-9743574891
+1-570-456-2233;+66-12324576
What it won't match:
91-9743574891
+15704562233
6612324576
How about this ^([0-9\-\+]{5,15};?)+$
Explanation:
^ #Match the start of the line
[0-9\-\+] #Allow any digit or a +/- (escaped)
{5,15} #Length restriction of between 5 and 15 (change as needed)
;? #An optional semicolon
+ #Pattern can be repeat once or more
$ #Until the end of the line
Only as restrictive as specified could be tighter, See it working here.
Your regex will match what you allow, but I would be a bit more restrictive:
^\+?[0-9-]+(?:;\+?[0-9-]+)*$
See it here on Regexr
That means match an optional "+" followed by a series of digits and dashes. Then there can be any amount of additional numbers starting with a semicolon, then the same pattern than for the first number.

Categories

Resources