0

I want to combine the two regex.

/^([a-z]{2})?([0-9]+)$/gi and /^(?!.*?([0-9])\1{3})\S+$/gi

something like this /(([a-z]{2})?([0-9]+)) || ((?!.*?([0-9])\1{3})\S+)/gi and this /(([a-z]{2})?([0-9]+)) && ((?!.*?([0-9])\1{3})\S+)/gi

So I need both the above two separate regex combined in one regex format.

I need both or(||) as well as and(&&).

what the above regex code actually does is:

Below is to check for, not allow more than 3 same character:

/^(?!.*?([0-9])\1{3})\S+$/gi.test("4444") => false
/^(?!.*?([0-9])\1{3})\S+$/gi.test("11222") => true
/^(?!.*?([0-9])\1{3})\S+$/gi.test("77722") => true

Below is for to check, not more than 2 characters at beginning and followed by only numbers:

/^(([a-z]{1,2})?([0-9]+))$/gi.test('sj76755') => true
/^(([a-z]{1,2})?([0-9]+))$/gi.test('k4545') => true
/^(([a-z]{1,2})?([0-9]+))$/gi.test('6j53') => false
/^(([a-z]{1,2})?([0-9]+))$/gi.test('653aa') => false
/^(([a-z]{1,2})?([0-9]+))$/gi.test('ss653aa') => false

So I need both the above two separate regex combined in one regex format.

I need both or(||) as well as and(&&).

Thanks in advance

3
  • Provide some valid and invalid matches in your question Commented Jun 22, 2020 at 18:57
  • It's hard to tell exactly what you're after here. As anubhava mentioned, some examples would be useful. From a strictly logical standpoint, if you need "logical and" and "logical or" at the same time, it effectively becomes just "logical and". Is it that you want 0000 or aa0000? Should aa0 pass? Commented Jun 22, 2020 at 19:03
  • this is dupr supr, same question dupr'd a week ago. of course since each regx is different it cant be just joined, yes, no ? Commented Jun 22, 2020 at 19:52

1 Answer 1

1

OP Curr regex's :
( ^(?!.*?([0-9])\1{3})\S+$ , ^(([a-z]{1,2})?([0-9]+))$ )

The AND :
note - The minimum is the subset that includes both is the only required, dissallow others.
In this case, the two regex minimums are ^\S+$ , ^[a-z]{0,2}[0-9]+$
where the subset that satisfies both is ^[a-z]{0,2}[0-9]+$.

After applying the assershun we find the AND regex resolves to this

^(?!.*?([0-9])\1{3})[a-z]{0,2}[0-9]+$

demo1

The OR :
note - Each regex has it's own pecularity's that make it dificult to
merge functionality. It is possible though to just factor out common
terms that satisfy both.
Each need to be joined with an or metachar |.
But, each situation is different, it takes an experienced developer (me)
to find the possibilities.

After factoring we find the OR regex resolves to this

^(?:(?!.*?([0-9])\1{3})\S+|[a-z]{0,2}[0-9]+)$

demo2


OP Prev regex's :
The OR ^([a-z]{2})?([0-9]+)$|[0-9]{4,}

The AND ^([a-z]{2})?([0-9]{4,})$

Sign up to request clarification or add additional context in comments.

2 Comments

I just updated my post. could you please help me out with that?
reviewd new post info, added insite

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.