1

Trying to check for < or > in the user input in javascript. any idea what the regular expression wud be? This regex does not seem to work

 var spclChar=/^[<>]$/;
        if(searchCriteria.firstName.match(spclChar)){
            return true;
        }else {
            return false;
        }
Bidisha
  • 287
  • 1
  • 5
  • 16

1 Answers1

2

You can use a character class like this:

[<>]
or
[><]

Working demo

Btw, you have useful comments in your question like Sam's

/[<>]/

https://regex101.com/r/oP0nG0/1

and Marc B's

your regex is searching for a < at the START of the string, or a > at the END of the string. if you want them ANYWHERE in the string, then dump the ^ and $. – Marc B 7 mins ago

Community
  • 1
  • 1
Federico Piazza
  • 30,085
  • 15
  • 87
  • 123