Reputation: 895
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
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 stringUpvotes: 2
Reputation: 1042
const pattern = /^[^"\\,]*$/
the leading ^
inside []
means anything except any chars following ^ sign inside []
Upvotes: 2
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