I'm trying to write a regex to test for certain special characters, but I think I am overcomplicating things. The characters I need to check for are: &<>'"
My current regex looks like such:
/&<>'"/
Another I was trying is:
/\\&\\<\\>\\'\\"/
Any tips for a beginner (in regards to regex)? Thanks!
You are looking for a character class :
/[&<>'"]/
In doing so, any of the characters in the square brackets will be matched.
The expression you were originally using, /&<>'"/
, wasn't working as expected because it matches the characters in that sequential order. In other words, it would match a full string such as &<>'"
but not &<
.
I'm assuming that you want to be able to match all of the characters you listed, at one time.
If so, you should be able to combine a character set with the g
(global-matching) flag, for your regex.
Here's what it could look like:
/[<>&'"]/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.