I'm using javascript to set the value of an input with text that may contain html specific chars such a &
etc. So, I'm trying to find one regex that will match these values and replace them with the appropriate value ("&", " ") respectively, only I can't figure out the regex to do it. Here's what I'm trying to do. Make an object the contains the matches and reference to the replacement value:
var specialChars = {
" " : " ",
"&" : "&",
">" : ">",
"<" : "<"
}
Then, I want to match my string
var stringToMatch = "This string has special chars & and "
I tried something like
stringToMatch.replace(/( |&)/g,specialChars["$1"});
but it doesn't work. I don't really understand how to capture the special tag and replace it. Any help is greatly appreciated.