vinod hy
vinod hy

Reputation: 895

Regular expression check to not allow certain characters

I have a requirement to not allow " , \ in the particular string. May i please know on how to write a regular expression.

Eg: employee name = "testName"; I need a pattern to check there are no " , \ at any position. Apart from these three characters, rest alll characters should be allowed.

I am new to regular expression. Kindly help me.

Upvotes: 0

Views: 60

Answers (3)

ruohola
ruohola

Reputation: 24018

The pattern for this is as simple as ^[^",\\]+$

Explanation:

  • ^ start of string
    • [ start character class
      • ^ any character but the following
      • ",\\ literal " or , or \
    • ] end character class
    • + one or more of the preceeding
  • $ end of string

Upvotes: 2

钵钵鸡实力代购
钵钵鸡实力代购

Reputation: 1042

const pattern = /^[^"\\,]*$/

the leading ^ inside [] means anything except any chars following ^ sign inside []

Upvotes: 2

Dorian B
Dorian B

Reputation: 150

You can test if the string does not match these characters :

let isValid = !name.match(/\\|,|"/)    // if ",\ are not in the string

Upvotes: 1

Related Questions