Sunday, November 9, 2014

remove smart quotes via javascript

I always have minor twinges that I am not typographically-morally upright enough to prefer "proper quotes" to "inch marks". 

But smart quote characters create more problems than they are worth. I used to run suspect text through the  Dan Hersam's Filter but I realized I should just take the basic code and put it more directly into my workflow. 

Here's the function I came up with, pass in the id of an input element and it will replace its contents with a safer, if less typographically pure, version.


function unsmartquote(id){
    var element = document.getElementById(id);
    var s = element.value;

    s = s.replace(/[\\u2018\\u2019]/g, "'");
    s = s.replace(/[\\u201C\\u201D]/g, '"');
    s = s.replace( /\\u2014/g, '--' );
    s = s.replace( /\\u2026/g, '...' );
    s = s.replace( /\\u00A9/g, '©' );
    s = s.replace( /\\u00AE/g, '®' );
    s = s.replace( /\\u2122/g, '<sup>TM</sup>' );
    s = s.replace( /\\u00BC/g, '<sup>1/4</sup>' );
    s = s.replace( /\\u00BD/g, '<sup>1/2</sup>' );
    s = s.replace( /\\u00BE/g, '<sup>3/4</sup>' );
    s = s.replace(/[\\u02DC|\\u00A0]/g, " ");
    element.value = s;

}

It took a little longer than I had expected to make this, I got bit by double slash for the unicode escape sequence merging into single slash by the time I was trying to copy and paste the code

No comments:

Post a Comment