Click here to Skip to main content
15,881,455 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi All,
I have one textbox in an angular application. We want to restrict some set of string from that textbox value. A regular expression check to restrict '&&', '||', '--'. '/*' to be inserted inside that string. I have tried many regular expression but not working.

What I have tried:

I tried many regex like below example
(&&)(--)(__)
(&&)|(--)|(__)
etc..
Posted
Updated 13-Jul-21 7:33am

This worked for me:
(&&)|(\|\|)|(--)|(__)|(/\*)
Bear in mind that that Regex will find those strings - so if you get a match you need to reverse the decision!
To get a match when none of the sequences are there, you'd need to use a negative lookahead:
^(?!.*(&&)|(\|\|)|(--)|(__)|(/\*)).*$
Which ./.. looks pretty impenetrable!
 
Share this answer
 
v2
Comments
Maciej Los 13-Jul-21 15:53pm    
5ed!
Sunil Kumar Pandab 14-Jul-21 9:18am    
Yes your solution is working as expected but ^(?!.*(&&)|(\|\|)|(--)|(__)|(/\*)).*$ all together it's not working. I divided regex to multiple validation like ^(?!.*(&&)).*$ and ^(?!.*(\|\|)).*$ and so on.
You can also try this way:
&&|\|\||--|__|/\*

groups are not necessary.

Just a few interesting links to help building and debugging RegEx.
Here is a link to RegEx documentation:
perlre - perldoc.perl.org[^]
Here is links to tools to help build RegEx and debug them:
.NET Regex Tester - Regex Storm[^]
Expresso Regular Expression Tool[^]
RegExr: Learn, Build, & Test RegEx[^]
Online regex tester and debugger: PHP, PCRE, Python, Golang and JavaScript[^]
This one show you the RegEx as a nice graph which is really helpful to understand what is doing a RegEx: Debuggex: Online visual regex tester. JavaScript, Python, and PCRE.[^]
This site also show the Regex in a nice graph but can't test what match the RegEx: Regexper[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900