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?
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.
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,'')
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.