Regular Expression to also allow slash "/" with existing Regex - javascript

I am facing difficulty to allow slash "/" with an existing regex
Below is an existing Regex which allows dot and numbers:
val.match(/^[0-9]+(\.[0-9]{1,2})?$/)
I changes it to...
val.match(/^[0-9]+([./][0-9\/]{1,2})?$/)
But this one won't allow the number like 1.5/384 where both dot/period and slash simultaneously.
Can someone help me with it?

You may add an optional non-capturing group after your main pattern part to match 1 or 0 occurrences of / followed with 1 or more digits:
/^\d+(?:\.\d{1,2})?(?:\/\d+)?$/
^^^^^^^^^^
See the regex demo
Details
^ - start of string
\d+ - 1 or more digits
(?:\.\d{1,2})? - an optional sequence of . and then 1 or 2 digits
(?:\/\d+)? - an optional sequence of / and then 1+ digits
$ - end of string.
If the number after / can be float in the same format as the first number:
/^\d+(?:\.\d{1,2})?(?:\/\d+(?:\.\d{1,2})?)?$/
^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^

This should do what you want :
^(\d+(?:\.\d{1,2})?\/?(?:\d+\.\d{1,2})?)$
See this Regex101.com
Edit : Corrected the fact that it didn't match 1 or 1.5

Related

What will be the RegEx one or more, Which can validate all WebSocket Urls ? is there any npm module for the same? [duplicate]

I'm trying to write a regex for valid websocket address but I couldn't figure out what's wrong. Here's what I've already tried
/(^ws:\/\/|^wss:\/\/)(([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\:)|([a-zA-Z]+))([0-9]{1,5})/gm
Regex should match below
ws://0.0.0.0:8080
wss://192.168.0.25:12345
ws://localhost:3333
You may use
/^(wss?:\/\/)([0-9]{1,3}(?:\.[0-9]{1,3}){3}|[a-zA-Z]+):([0-9]{1,5})$/
See the regex demo
Note that [a-zA-Z]+ won't be enough as it only matches ASCII letters.
Use [^\/]+ instead if you want to match any chars but /:
/^(wss?:\/\/)([0-9]{1,3}(?:\.[0-9]{1,3}){3}|[^\/]+):([0-9]{1,5})$/
See this regex demo.
To just match FQDNs, use
/^(wss?:\/\/)([0-9]{1,3}(?:\.[0-9]{1,3}){3}|(?=[^\/]{1,254}:[0-9]{1,5}$)(?:(?=[a-zA-Z0-9-]{1,63}\.)(?:xn--+)?[a-zA-Z0-9]+(?:-[a-zA-Z0-9]+)*\.)+[a-zA-Z]{2,63}):([0-9]{1,5})$/
Adaped from this solution by Tim Pietzcker. There are all the explanations in his post. See this regex demo.
The main problem was with :: it was only matched after numeric IP pattern, not after the [a-zA-Z]+.
Details
^ - start of string
(wss?:\/\/) - ws:// or wss://
([0-9]{1,3}(?:\.[0-9]{1,3}){3}|[a-zA-Z]+) - 1 to 3 digits followed with 3 occurrences of . and 1 to 3 digits or 1+ letters (or, if [^\/]+ is used, 1+ chars other than /)
: - :
([0-9]{1,5}) - 1 to 5 digits
$ - end of string.
The second alternation does not match the :
You could move it outside of the alternation to match it for both of the options.
If you don't need the capturing groups, you could omit them.
The first alternation could be shortened making the extra s optional:
^(wss?:\/\/)(([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3})|([a-zA-Z]+)):[0-9]{1,5}$
Regex demo

regex for simple arithmetic expression

I've read other stackoverflow posts about a simple arithmetic expression regex, but none of them is working with my issue:
I need to validate this kind of expression: "12+5.6-3.51-1.06",
I tried
const mathre = /(\d+(.)?\d*)([+-])?(\d+(.)?\d*)*/;
console.log("12+5.6-3.51-1.06".match(mathre));
but the result is '12+5', and I can't figure why ?
You only get 12.5 as a match, as there is not /g global flag, but if you would enable the global flag it will give partial matches as there are no anchors ^ and $ in the pattern validating the whole string.
The [+-] is only matched once, which should be repeated to match it multiple times.
Currently the pattern will match 1+2+3 but it will also match 1a1+2b2 as the dot is not escaped and can match any character (use \. to match it literally).
For starting with digits and optional decimal parts and repeating 1 or more times a + or -:
^\d+(?:\.\d+)?(?:[-+]\d+(?:\.\d+)?)+$
Regex demo
If the values can start with optional plus and minus and can also be decimals without leading digits:
^[+-]?\d*\.?\d+(?:[-+][+-]?\d*\.?\d+)+$
^ Start of string
[+-]? Optional + or -
\d*\.\d+ Match *+ digits with optional . and 1+ digits
(?: Non capture group
[-+] Match a + or -
[+-]?\d*\.\d+ Match an optional + or - 0+ digits and optional . and 1+ digits
)+ Close the noncapture group and repeat 1+ times to match at least a single + or -
$ End of string
Regex demo
You would try to use this solution for PCRE compatible RegExp engine:
^(?:(-?\d+(?:[\.,]{1}\d)?)[+-]?)*(?1)$
^ Start of String
(?: Non capture group ng1
(-?\d+(?:[\.,]{1}\d)?) Pattern for digit with or without start
"-" and with "." or "," in the middle, matches 1 or 1.1 or 1,1
(Matching group 1)
[+-]? Pattern for "+" or "-"
)* Says
that group ng1 might to repeat 0 or more times
(?1) Says that
it must be a digit in the end of pattern by reference to the first subpattern
$ End of string
As JS does not support recursive reference, you may use full version instead:
/^(?:(-?\d+(?:[\.,]{1}\d)?)[+-]?)*(-?\d+(?:[\.,]{1}\d)?)$/gm

Can't get RegEx pattern to work correctly in MVC 5 View using Javascript

Here is my pattern. I'm trying to allow numbers and a decimal with two places plus an optional comma with three digits.
var pattern = /^[0-9]+(,\d{3})*\.[0-9]{2}$/;
Allow
100,000.12
10,000.12
1,000.12
100.12
10.12
.12 (can't get this to allow... see below)
Don't allow
abcd
1,,000.12
1,00.12
1,000.0
1,000.
1,000
Here is the test. If I add a ? after [0-9] it works here, but it does not work in my MVC 5 View. The modal doesn't open, so MVC doesn't like it.
^[0-9]?+(,\d{3})*\.[0-9]{2}$
https://regex101.com/r/HwLS7q/1
UPDATE 1
Don't allow
000,000.12, 0.12 etc...
Any help is much appreciated! Thanks!
[0-9]?+ is a pattern that matches 1 or 0 digits possessively, not allowing backtracking into the pattern. JS regex does not support possessive quantifiers, hence the issue.
You need to use
^[0-9]*(?:,[0-9]{3})*\.[0-9]{2}$
Or
^(?:[0-9]+(?:,[0-9]{3})*)?\.[0-9]{2}$
Here, the [0-9]* match zero or more digits and (?:[0-9]+(?:,[0-9]{3})*)? matches an optional sequence of 1+ digits followed with 0+ repetitions of , and 3 digit groups.
See this regex demo.
A more precise pattern would be to restrict the first digit chunk to 1, 2 or 3 digits and make the integer part optional:
^(?:[0-9]{1,3}(?:,[0-9]{3})*)?\.[0-9]{2}$
See the regex demo.
Details
^ - start of string
(?:[0-9]{1,3}(?:,[0-9]{3})*)? - an optional sequence of
[0-9]{1,3} - one to three digits
(?:,[0-9]{3})* - 0 or more repetitions of
, - comma
[0-9]{3} - three digits
\. - dot
[0-9]{2} - two digits
$ - end of string.

How do I enforce that certain characters must be present when there is an optional character before them?

I would like to capture a string that meets the criteria:
may be empty
if it is not empty it must have up to three digits (-> \d{1,3})
may be optionally followed by a uppercase letter ([A-Z]?)
may be optionally followed by a forward slash (i.e. /) (-> \/?); if it is followed by a forward slash it must have from one to three digits
(-> \d{1,3})
Here's a valid input:
35
35A
35A/44
Here's invalid input:
34/ (note the lack of a digit after '/')
I've come up with the following ^\d{0,3}[A-Z]{0,1}/?[1,3]?$ that satisfies conditions 1-3. How do I deal with 4 condition? My Regex fails at two occassions:
fails to match when there is a digit and a forward slash and a digit e.g .77A/7
matches but it shouldn't when there isa digit and a forward slash, e.g. 77/
You may use
/^(?:\d{1,3}[A-Z]?(?:\/\d{1,3})?)?$/
See the regex demo
Details
^ - start of string
(?:\d{1,3}[A-Z]?(?:\/\d{1,3})?)? - an optional non-capturing group:
\d{1,3} - one to three digits
[A-Z]? - an optional uppercase ASCII letter
(?:\/\d{1,3})? - an optional non-capturing group:
\/ - a / char
\d{1,3} - 1 to 3 digits
$ - end of string.
Visual graph (generated here):
This should work. You were matching an optional slash and then an optional digit from 1 to 3; this matches an optional combination of a slash and 1-3 of any digits. Also, your original regex could match 0 digits at the beginning; I believe that this was in error, so I fixed that.
var regex = /^(\d{1,3}[A-Z]{0,1}(\/\d{1,3})?)?$/g;
console.log("77A/7 - "+!!("77A/7").match(regex));
console.log("77/ - "+!!("77/").match(regex));
console.log("35 - "+!!("35").match(regex));
console.log("35A - "+!!("35A").match(regex));
console.log("35A/44 - "+!!("35A/44").match(regex));
console.log("35/44 - "+!!("35/44").match(regex));
console.log("34/ - "+!!("34/").match(regex));
console.log("A/3 - "+!!("A/3").match(regex));
console.log("[No string] - "+!!("").match(regex));

RegExp - How to check for both parentheses in the string

I'd like to know how can I match text ONLY if there are both parentheses (starting and closing).
Currently, my RegExp is: ^1?\s?\(?\d{3}\)?[-\s]?\d{3}[-\s]?\d{4}$.
It checks for US valid numbers, however \(? and \)? makes the difference. It should match the text if there is 0 of them or 2.
Some tests:
1 555)555-5555 - false
555)-555-5555 - false
(555-555-5555 - false
Now, they all return the same result - true.
Here is the link to it:
https://regexr.com/3kjei (^ is because I have to select only one string without any line break, please remove 2 other strings from it so you can see it returns true. All of the strings in this link should return true, just check it so only one string is in the text editor).
Thank you
You may use
^(?:1\s?)?(?:\(\d{3}\)|\d{3})[-\s]?\d{3}[-\s]?\d{4}$
See the regex demo.
Note that ^1?\s? in your regex allow a single whitespace in the beginning, that is why I suggest ^(?:1\s?)? - an optional sequence starting with 1 that is optionally followed wih whitespace.
The \(?\d{3}\)? part is replaced with (?:\(\d{3}\)|\d{3}) - a non-capturing group that matches either (+3 digits+) or 3 digits (so, no (123 or 123)` can be matched).
Details
^ - start of string
(?:1\s?)? - 1 or 0 occurrences of 1 optionally followed with 1 whitespace char
(?:\(\d{3}\)|\d{3}) - either (, 3 digits, ) or just 3 digits
[-\s]? - an optional - or whitespace
\d{3} - 3 digits
[-\s]?- an optional - or whitespace
\d{4} - 4 digits
$ - end of string.
Why not in the portion of your regex that says \(?\d{3}\)? that you "or" it instead.
Eg. Replace it with something like this:
((\(\d{3}\))|\d{3})

Categories