How do I modify my Javascript Regular expression to return the string: “””Collawan Annals of Plastic Surger Jan 1999 vol 42 pg 21 26.”””?
How do I properly escape a double quote in a regexp range?
(attempt in Firebug):
>>> var input="Collawn \"Annals of Plastic Surgery\" Jan 1999 vol 42 pg 21 26"
>>> input.replace(/[\.,:\[\]-]/g, ' ');
"Collawn "Annals of Plastic Surgery" Jan 1999 vol 42 pg 21 26"
>>> input.replace(/[\.,:\[\]-\"]/g, ' ');
SyntaxError: invalid range in character class { message="invalid range in character class", more...}
The problem is not the
"but the-– if you want it to mean a literal dash, you need to put it at the start or the end of the character class:Otherwise
A-Zmeans “any character from A to Z”, and your regex contained the equivalent ofZ-Awhich is an invalid range ([-"would be ASCII 91 to 34).