0

I am new to regex I want to create regex for following

AL--->accepted
AL100--->accepted
100---->accepted
100L--->Rejected
AL1122--->accepted
AL1224K-->rejected

Means either its AL or AL with numbers or only numbers

Below is my Regex which I have written but it's not working

^AL[0-9]+$|AL

Please Help

Thanks

1 Answer 1

2

I think you can just slightly modify the regex you already have to this:

^AL[0-9]*$

This will match AL in isolation (no digits), or followed by any number of digits, but nothing else.

Demo:

/^AL[0-9]*$/.test('AL1224K');
false

/^AL[0-9]*$/.test('AL1224');
true

Update:

If you also want to accept pure numbers, then you can modify your regex by making the leading AL optional:

^(AL)?[0-9]*$

E.g.

/^(AL)?[0-9]*$/.test('AL1224')
true
/^(AL)?[0-9]*$/.test('1224')
true
Sign up to request clarification or add additional context in comments.

2 Comments

Hey what if i have to make number also to be expected /^AL[0-9]*$/.test('100');
If an empty string is not expected, use ^(AL[0-9]*|[0-9]+)$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.