Forum Moderators: open
Does anyone have a good js quotemeta [php.net] routine they are willing to share? Can't seem to find one out there.
bs=String.fromCharCode(92);
unsafe=bs+".+*?[^]$(){}=!<>¦:";
for (i=0;i<unsafe.length;++i){
safesearch=safesearch.replace(unsafe.charAt(i),bs+unsafe.charAt(i));} (where safesearch is the search terms you want to protect)
"only+first+instance" --> "only\+first+instance" There is, no doubt, no regular expression usage that will do this at a stroke, without looping, but I don't know what it is.
Here's my amendment to your solution anyway.
bs=String.fromCharCode(92);
unsafe=bs+".+*?[^]$(){}=!<>¦:";
for (i=0;i<unsafe.length;++i){
safesearch=safesearch.replace(new RegExp("\\"+unsafe.charAt(i),"g"),bs+unsafe.charAt(i));
}