1

First letter should be alpha/numeric and forward slash, every special characters are allowed except & and |

 new RegExp('^[\/A-Za-z0-9](([^\|\&]?[a-zA-Z0-9]?)*)$');

Above regex work for correct format and minimum characters e.g(/abcd%*)

If i give long character e.g( /abcdabcdabcdabcdabcdabcdabcd& ),stop script error occurs in firefox. Please suggest some better regex for fix it

Demo

2
  • It would be better if you tell us what string(s) you are trying to match. Commented Oct 8, 2015 at 11:06
  • @SalmanA Just look at the fiddle or the post. He's trying to match with /abcdabcdabcdabcdabcdabcdabcd& Commented Oct 8, 2015 at 11:08

1 Answer 1

4

Your issue is the capturing groups (brackets). Get rid of those and it's fine. Just make it as simple as you can!

First letter should be alpha/numeric [or] forward slash

^[/A-Za-z0-9]

every special characters are allowed except & and |

[^|&]+

(You don't need to escape | or &.)

Putting it together:

^[\/A-Za-z0-9][^|&]+$

Try it here.

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

3 Comments

Besides the capturing groups, there is also a potential catastrophic backtracking issue with the original regex.
Don't need to escape / either (inside first bracket).
Oh yeah! I always thought you did... :)

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.