js regex not working. Exact match and or operation - javascript

The first 3 characters needs to be:
Exactly either ABC or ACD or BCD
Then followed be a hyphen -
Then followed by either a 5 or 8
Then any 4 numbers
Examples:
ABC-56789 (True)
AAA-56789 (False)
I have tried this:
/^[^ABC$|^ACD$|^BCD$][*-][5|8][0-9]{4}$/

How about use this expression?
^(ABC|ACD|BCD)-[5|8]\d{4}$
[] means character set. So, [ABC] means any A or B or C, not ABC.
And ^ means negated in []. So, regex you used may not work fine.
If you want to group the tokens, you should use ().
You can also use \d (digit) instead of [0-9].

Use this regex:
const regex = /^(?:ABC|ACD|BCD)-[58][0-9]{4}$/;
[
'ABC-56789',
'AAA-56789'
].forEach(str => {
console.log(str, '==>', regex.test(str));
})
Output:
ABC-56789 ==> true
AAA-56789 ==> false
Explanation of regex:
^ -- anchor at beginning of string
(?:ABC|ACD|BCD) -- non-capture group with OR combinations
- -- literla dash
[58] -- a 5 or 8
[0-9]{4} -- four digits
$ -- anchor at end of string
Learn more about regex: https://twiki.org/cgi-bin/view/Codev/TWikiPresentation2018x10x14Regex

Use parentheses, not square brackets, to group alternation patterns:
^(ABC|ACD|BCD)-[58][0-9]{4}$

You have to change the regex to the following:
/^(ABC|ACD|BCD)-(5|8)[0-9]{4}$/
[] match single characters, but you want to match three characters in the beginning, so you have to use the () to create a capturing group.

Using a single alternation:
^(?:ABC|[AB]CD)-[58][0-9]{4}$
Explanation
^ Start of string
(?: Non capture group for the alternatives
ABC Match literally
| Or
[AB]CD Match either ACD or BCD
) Close the non capture group
- Match literally
[58] Match either 5 or 8`
[0-9]{4} Match 4 digits 0-9
$ End of string
See a regex101 demo

Related

Javascript Regex to match only zero/even number of backslashes anywhere in a string

I need a regular expression that matches the complete string with a zero/even number of backslashes anywhere in the string. If the string contains an odd number of backslashes, it should not match the complete string.
Example:
\\ -> match
\\\ -> does not match
test\\test -> match
test\\\test-> does not match
test\\test\ -> does not match
test\\test\\ -> match
and so on...
Note: We can assume any string of any length in place of 'test' in the above example
I am using this ^[^\\]*(\\\\)*[^\\]*$ regular expression, but it does not match the backslashes after the second test.
For example:
test\\test(doesn't match anything after this)
Thanks for any help in advance.
You may use this regex:
^(?:(?:[^\\]*\\){2})*[^\\]*$
RegEx Demo
RegEx Breakdown:
^: Start
(?:: Start non-capture group #1
(?:: Start non-capture group #2
[^\\]*: Match 0 or more og any char except a \
\\: Match a \
){2}: End non-capture group #2. Repeat this group 2 times.
)*: End non-capture group #1. Repeat this group 0 or more times.
[^\\]*: Match 0 or more og any char except a \
$: End
The current regular expression ^[^\\]*(\\\\)*[^\\]*$ can be interpreted as Any(\\)*Any, Where Any means any character except backslash.
The expected language shall be Any\\Any\\Any\\..., which can be obtained by containing the current regular expression in Kleene closure operator. That is (Any(\\)*Any)*
The original regular expression after modification:
^([^\\]*(\\\\)*[^\\]*)*$
It can be further optimized as:
^((\\\\)*[^\\]*)*$

How do I combine these three regex into one?

I'm trying to make a regular expression that only accepts:
Min 100 and atleast 1000 characters, characters “,’,<,> aren't allowed, two full stops one after another aren't allowed.
This is what I have for now:
^.{100,1000}$ → for 100 to 1000 characters
^[^"'<>]*$ → for the characters that aren't allowed
^([^._]|[.](?=[^.]|$)|_(?=[^_]|$))*$ → doesn't allow 2 consecutive dots
How do I combine this regex into one? ._.
This part [^._] means no dot or underscore and this part [.](?=[^.]|$)|_(?=[^_]|$) matches either a . or _ followed by the opposite or end of string.
You could write the pattern using a single negative lookahead assertion excluding __ or ..
^(?!.*([._])\1)[^"'<>\n]{100,1000}$
Explanation
^ Start of string
(?! Negative lookahead, assert that what is at the right is not
.*([._])\1 capture either . or _ and match the same captured char after it (meaning no occurrence of .. or __)
) Close lookahead
[^"'<>\n]{100,1000} Match 100-1000 times any character except the listed
$ End of string
Regex demo (with the quantifier set to {10,100} for the demo)

Regex match any js number

So as an exercise I wanted to match any JS number. This is the one I could come up with:
/^(-?)(0|([1-9]\d*?|0)(\.\d+)?)$/
This however doesn't match the new syntax with underscore separators (1_2.3_4). I tried a couple of things but I couldn't come up with something that would work. How could I express all JS numbers in one regex?
For the format in the question, you could use:
^-?\d+(?:_\d+)*(?:\.\d+(?:_\d+)*)?$
See a regex demo.
Or allowing only a single leading zero:
^-?(?:0|[1-9]\d*)(?:_\d+)*(?:\.\d+(?:_\d+)*)?$
The pattern matches:
^ Start of string
-? Match an optional -
(?:0|[1-9]\d*) Match either 0 or 1-9 and optional digits
(?:_\d+)* Optionally repeat matching _ and 1+ digits
(?: Non capture group
\.\d+(?:_\d+)* Match . and 1+ digits and optionally repeat matching _ and 1+ digits
)? Close non capture group
$ End of string
See another regex demo.
how about this?
^(-?)(0|([1-9]*?(\_)?(\d)|0|)(\.\d+)?(\_)?(\d))$

Regex of three optional parts with separators

I am trying to parse the three optional components of a string with this format any of these different combinations:
12345,abcd#ABCD -> $1=12345 $2=abcd $3=ABC
12345,abcd -> $1=12345 $2=abcd $3=empty
12345#ABCD -> $1=12345 $2=empty $3=ABC
12345 -> $1=12345 $2=emty $3=empty
Is it possible with a single regexp?
I have done several attempts. When the string is complete no problem but the forms with parameters missing are escaping to me:
(.+),(.+)#(.+) // works when the string is complete
// but how do you express the optionality?
(.+),?(.+)#?(.+) // nope
(.*)[$,](.*)[$#](.*) // neither
(Another option, would be splitting the string into the components that looks quite trivial but I am curious about the regexp way)
12345,abcd#ABCD -> $1=12345 $2=abcd $3=ABC
12345,abcd -> $1=12345 $2=abcd $3=empty
12345#ABCD -> $1=12345 $2=empty $3=ABC
12345 -> $1=12345 $2=emty $3=empty
From your expected output it appears that you want empty groups in your matches while matching your inputs. You may use this regex:
/^(\d+),?([^#\n]*)#?(.*)$/g
RegEx Demo
Note that this regex will always return 3 captured groups in every match result.
RegEx Details:
^: Start
(\d+): Match 1+ digits and capture in group #1
,?: Match an optional comma
([^#]*): Match 0+ any character that is not # and capture in group #2
#?: Match an optional #
(.*): Match 0+ any character and capture in group #3
$: End
You can use
^([^,#]+)(?:,([^#]+))?(?:#(.+))?$
See the regex demo (note there are newlines added in the demo pattern since the test is performed against a single multiline string there, in real world, the strings to test won't contain newlines, hence they are not in the pattern here.)
Details
^ - start of string
([^,#]+) - Group 1: one or more chars other than a comma and #
(?:,([^#]+))? - an optional non-capturing group matching 1 or 0 occurrences of a comma and then (capturing into Group 2) any one or more chars other than #
(?:#(.+))? - an optional non-capturing group matching 1 or 0 occurrences of a # char and then (capturing into Group 3) any one or more chars other than line break chars as many as possible
$ - end of string.

Capture between pattern of digits

I'm stuck trying to capture a structure like this:
1:1 wefeff qwefejä qwefjk
dfjdf 10:2 jdskjdksdjö
12:1 qwe qwe: qwertyå
I would want to match everything between the digits, followed by a colon, followed by another set of digits. So the expected output would be:
match 1 = 1:1 wefeff qwefejä qwefjk dfjdf
match 2 = 10:2 jdskjdksdjö
match 3 = 12:1 qwe qwe: qwertyå
Here's what I have tried:
\d+\:\d+.+
But that fails if there are word characters spanning two lines.
I'm using a javascript based regex engine.
You may use a regex based on a tempered greedy token:
/\d+:\d+(?:(?!\d+:\d)[\s\S])*/g
The \d+:\d+ part will match one or more digits, a colon, one or more digits and (?:(?!\d+:\d)[\s\S])* will match any char, zero or more occurrences, that do not start a sequence of one or more digits followed with a colon and a digit. See this regex demo.
As the tempered greedy token is a resource consuming construct, you can unroll it into a more efficient pattern like
/\d+:\d+\D*(?:\d(?!\d*:\d)\D*)*/g
See another regex demo.
Now, the () is turned into a pattern that matches strings linearly:
\D* - 0+ non-digit symbols
(?: - start of a non-capturing group matching zero or more sequences of:
\d - a digit that is...
(?!\d*:\d) - not followed with 0+ digits, : and a digit
\D* - 0+ non-digit symbols
)* - end of the non-capturing group.
you can use or not the ñ-Ñ, but you should be ok this way
\d+?:\d+? [a-zñA-ZÑ ]*
Edited:
If you want to include the break lines, you can add the \n or \r to the set,
\d+?:\d+? [a-zñA-ZÑ\n ]*
\d+?:\d+? [a-zñA-ZÑ\r ]*
Give it a try ! also tested in https://regex101.com/
for more chars:
^[a-zA-Z0-9!##\$%\^\&*)(+=._-]+$

Categories