Match a Particular set of string with regex - javascript

I am trying to match a particular set of strings with a regex
1- #1 – .75 Gallon $16.99
2- #2 –1.6 Gallon $36.99
This is what I tried to figure out with many attempts but still it doesn't seems to work
console.log(/^#\d\s+–\s+[0-9]*\.[0-9]+\s+[a-zA-Z]+\s+:[0-9]*\.[0-9]+$/.test('#2 – 1.6 Gallon $36.99'))
console.log(/^#\d\s+–\s+[0-9]*\.[0-9]+\s+[a-zA-Z]+\s+:[0-9]*\.[0-9]+$/.test('#1 – .75 Gallon $16.99'))
I have gone through each part individually but I don't know where I am making mistake ,any help would be really appreciated.
Thanks

You should allow any (even zero) amount of whitespaces around the hyphen, and you need to match a dollar symbol instead of a colon:
^#\d\s*–\s*\d*\.?\d+\s+[a-zA-Z]+\s+\$\d*\.?\d+$
See the regex demo.
I also added a ? quantifier after \. to match integers.
Details:
^ - start of string
# - a # char
\d - a digit
\s*–\s* - a hyphen wrapped with zero or more whitespaces
\d*\.?\d+ - an integer or float like value: zero or more digits, an optional . and then one or more digits
\s+ - one or more whitespaces
[a-zA-Z]+ - one or more letters
\s+ - one or more whitespaces
\$ - a $ char
\d*\.?\d+ - an integer or float like value
$ - end of string.

Related

Regex for input with numbers and commas

I'm trying to limit input data.
My goal:
only two symbols per input allowed: numbers and a comma
first symbol only number (zero or more)
amount of numbers is unlimited (zero or more)
a dangling comma is allowed but only one
Test cases:
1,2,4 - ок
1221,212,4121212 - ок
,2,3 - not ок
1,2,3, - ок
11,21111,31111, - ок
I've hade something like this but it doesn't work properly
/^\d*(,\d*)*$/.test(value)
Appreciate any help!
You can use
/^(?:\d+(?:,\d+)*,?)?$/
See the regex demo. Details:
^ - start of string
(?:\d+(?:,\d+)*,?)? - an optional non-capturing group:
\d+ - one or more digits
(?:,\d+)* - zero or more sequences of a comma and one or more digits
,? - an optional comma
$ - end of string.

Javascript Regular Expresion [duplicate]

I'm trying to write a RegExp to match only 8 digits, with one optional comma maybe hidden in-between the digits.
All of these should match:
12345678
12,45678
123456,8
Right now I have:
^[0-9,]{8}
but of course that erroneously matches 012,,,67
Example:
https://regex101.com/r/dX9aS9/1
I know optionals exist but don't understand how to keep the 8 digit length applying to the comma while also keeping the comma limited to 1.
Any tips would be appreciated, thanks!
To match 8 char string that can only contain digits and an optional comma in-between, you may use
^(?=.{8}$)\d+,?\d+$
See the regex demo
The lookahead will require the string to contain 8 chars. ,? will make matching a comma optional, and the + after \d will require at least 1 digit before and after an optional comma.
If you need to match a string that has 8 digits and an optional comma, you can use
^(?:(?=.{9}$)\d+,\d+|\d{8})$
See the regex demo
Actually, the string will have 9 characters in the string (if it has a comma), or just 8 - if there are only digits.
Explanation:
^ - start of string
(?:(?=.{9}$)\d+,\d+|\d{8}) - 2 alternatives:
(?=.{9}$)\d+,\d+ - 1+ digits followed with 1 comma followed with 1+ digits, and the whole string matched should be 9 char long (8 digits and 1 comma)
| - or
\d{8} - 8 digits
$ - end of string
See the Java code demo (note that with String#matches(), the ^ and $ anchors at the start and end of the pattern are redundant and can be omitted since the pattern is anchored by default when used with this method):
List<String> strs = Arrays.asList("0123,,678", "0123456", // bad
"01234,567", "01234567" // good
);
for (String str : strs)
System.out.println(str.matches("(?:(?=.{9}$)\\d+,\\d+|\\d{8})"));
NOTE FOR LEADING/TRAILING COMMAS:
You just need to replace + (match 1 or more occurrences) quantifiers to * (match 0 or more occurrences) in the first alternative branch to allow leading/trailing commas:
^(?:(?=.{9}$)\d*,\d*|\d{8})$
See this regex demo
You can use following regex if you want to let trailing comma:
^((\d,?){8})$
Demo
Otherwise use following one:
^((\d,?){8})(?<!,)$
Demo
(?<!,) is a negative-lookbehind.
/^(?!\d{0,6},\d{0,6},\d{0,6})(?=\d[\d,]{6}\d).{8}$/
I guess this cooperation of positive and negative look-ahead does just what's asked. If you remove the start and end delimiters and set the g flag then it will try to match the pattern along decimal strings longer than 8 characters as well.
Please try http://regexr.com/3d63m
Explanation: The negative look ahead (?!\d{0,6},\d{0,6},\d{0,6}) tries not to find any commas side by side if they have 6 or less decimal characters in between while the positive look ahead (?=\d[\d,]{6}\d) tries to find 6 decimal or comma characters in between two decimal characters. And the last .{8} selects 8 characters.

Issues in password regular expression

Hi all I am making a password regular expression in javascript test() method, It will take the following inputs
solution
/^(?=.*\d)^(?=.*[!#$%'*+\-/=?^_{}|~])(?=.*[A-Z])(?=.*[a-z])\S{8,15}$/gm
May contains any letter except space
At least 8 characters long but not more the 15 character
Take at least one uppercase and one lowercase letter
Take at least one numeric and one special character
But I am not able to perform below task with (period, dot, fullStop)
(dot, period, full stop) provided that it is not the first or last character, and provided also that it does not appear two or more times consecutively.
Can anyone one help me to sort out this problem, Thanks in advance
You may move the \S{8,15} part with the $ anchor to the positive lookahead and place it as the first condition (to fail the whole string if it has spaces, or the length is less than 8 or more than 15) and replace that pattern with [^.]+(?:\.[^.]+)* consuming subpattern.
/^(?=\S{8,15}$)(?=.*\d)(?=.*[!#$%'*+\/=?^_{}|~-])(?=.*[A-Z])(?=.*[a-z])[^.]+(?:\.[^.]+)*$/
See the regex demo
Details:
^ - start of string
(?=\S{8,15}$) - the first condition that requires the string to have no whitespaces and be of 8 to 15 chars in length
(?=.*\d) - there must be a digit after any 0+ chars
(?=.*[!#$%'*+\/=?^_{}|~-]) - there must be one symbol from the defined set after any 0+ chars
(?=.*[A-Z]) - an uppercase ASCII letter is required
(?=.*[a-z]) - a lowercase ASCII letter is required
[^.]+(?:\.[^.]+)* - 1+ chars other than ., followed with 0 or more sequences of a . followed with 1 or more chars other than a dot (note that we do not have to add \s into these 2 negated character classes as the first lookahead already prevalidated the whole string, together with its length)
$ - end of string.

Javascript RegExp - it include space and brackets around

Bit of a noob to regexp
Please check out my attempt.
I want to isolate numbers that do not have hyphen or other characters around them apart from brackets - and then place quotes around these digits
so far I have - [^a-z-0-9](\d+)[^0-9-a-z]
match group of digits - that does not start or end with numbers or charachters
It is currently matching (1, 2) instead of say 1 and 2
Test
(0-hyphen-number) OR
(123 no hyphen) OR
(no hyphen 2) OR
(no 3 hyphen) OR
(no -4- hyphen) OR
(no -5 hyphen) OR
(no 6- hyphen) OR
(blah 0987 hyp1hen) OR
(blah -4321 hyp-2hen) OR
(blah -1234- hyp3-hen)
Expected ouput :)
(0-hyphen-number) OR
("123" no hyphen) OR
(no hyphen "2") OR
(no "3" hyphen) OR
(no -4- hycphen) OR
(no -5 hyphden) OR
(no 6- hyphen) OR
(blah "0987" hyp1hen) OR
(blah -4321 hyp-2hen) OR
(blah -1234- hyp3-hen)
Your regex is close enough. You should however put - either at end or at beginning or character class.
You should capture all groups and replace them as follows.
Regex: ([^a-z0-9-])(\d+)([^0-9a-z-])
Replacement to do: Replace with $1"$2"$3
Regex101 Demo
do not have hyphen or other characters around them apart from brackets
You should take note that your original regex [^a-z-0-9](\d+)[^0-9-a-z]
matches any punctuation around the digits.
So, ,888+ and ,888] or *888} will match.
But what you're probably looking for is something like this
(?:^|[\s()])(\d+)(?:[\s()]|$)
which only allows whitespace boundary or parenth's boundary.
Change [\s()] to [\s(] or [\s}] to suite your needs.
Modification: To get possibly whitespace separated numbers as well.
https://regex101.com/r/pO4mO1/3
(?:^|[\s()])(\d+(?:\s*\d)*)(?:[\s()]|$)
Expanded
(?:
^
| [\s()]
)
( # (1 start)
\d+
(?: \s* \d )*
) # (1 end)
(?:
[\s()]
| $
)
By the time I loaded Regex101, it already had this working regex: [^a-z-0-9](\d+)[^0-9-a-z]
FYI (for everyone confused), in earlier revisions of the post, the regex was ^a-z-0-9[^0-9-a-z]. Another user edited the post to reflect what they saw in the demo.

how to accept negative values for amount textfield with this regular expression

I want to accept a negative value to the text field by without disturbing the functionality for following regular expression :
(?!^0*$)(?!^0*[.]0*$)^[0-9]{1,8}([.][0-9]{1,2})?$
for ex : -12.12, -1223233.23, -32323.32
Thanks.
Your regex has lookaheads that are each triggering at every location inside a string. To make the regex more efficient and easily adjustable for a fix like the one you need, you need to move the ^ out of the lookaheads: ^(?!0*$)(?!0*[.]0*$)^[0-9]{1,8}([.][0-9]{1,2})?$.
Now, you need to add an optional minus at the start. "Optional" means 1 or 0 occurrences. In JavaScript, you can use a ? quantifier for that (in POSIX BRE, you would have no other alternative but \{0,1\}).
So, use
^-?(?!0*$)(?!0*[.]0*$)[0-9]{1,8}([.][0-9]{1,2})?$
See the regex demo
The regex breakdown:
^ - start of string
-? - 1 or 0 hyphens
(?!0*$) - make sure there are no zeros up to the end of string (return no match if a zero is found)
(?!0*[.]0*$) - make sure there are no zeros + a dot + zeros up to the end of string
[0-9]{1,8} - match 1 to 8 digits
([.][0-9]{1,2})? - 1 or 0 sequences of...
[.] - a literal dot
[0-9]{1,2} - 1 to 2 digits
$ - end of string.
Just add -? to make the negative sign optional, or simply - if it's mandatory.
(?!^-?0*$)(?!^-?0*[.]0*$)^-?[0-9]{1,8}([.][0-9]{1,2})?$
Edit: Fixed
THis validates an integer (positive or negative)
^-{0,1}\d+$
This validate a decimal number (positive or negative)
^-{0,1}\d*\.{0,1}\d+$

Categories

Resources