Reputation: 2437
I have the following regular expression
/[^a-zA-Z0-9 ]/g
I want include "
in the following piece of code:
onKeyUp="this.value=this.value.replace(/[^a-zA-Z0-9"]/g,'')
I used the above regex.
The problem I am facing over here is that the above regular expression allows me to include other special characters (&
, *
, etc.) into it. How do I avoid it?
Upvotes: 0
Views: 235
Reputation: 655169
Just put the characters into the character class while taking the correct encoding into account:
onKeyUp="this.value=this.value.replace(/[^a-zA-Z0-9"&*]/g,'')"
Here you need to encode "
and &
with the character references "
and &
as you’re describing the value of an HTML attribute value declaration.
Upvotes: 2
Reputation: 12809
One first obvious issue with the code is that the "
that is part of the regex isn't escaped despite it being inside a "
-delimited string literal. You may want to try and change it to:
onKeyUp="this.value=this.value.replace(/[^a-zA-Z0-9\" ]/g,'')
Upvotes: 1