Last active
December 7, 2016 13:34
-
-
Save ciolt/bf6e4eec0f9d4dd6b67f5fb9cad9eba4 to your computer and use it in GitHub Desktop.
Convert dumb quotes to smart quotes and add em dashes where needed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
String.prototype.Smartify = function () { | |
var base = this.valueOf(); // gets the value of the original string | |
var _a = new RegExp(/([\"]([a-zA-Z0-9\s\S]{0,}?)([\"]))/, 'gm'); // filter for large chunks of dumb quotes ["which is shit like this"] | |
var _b = new RegExp(/([^\"]([a-zA-Z0-9\s\S]{0,}?)([^\"]))/, 'gm'); // sub-filter for everything that isn't dumb quotes [which is shit like this] | |
var _c = new RegExp(/([0-9]){1,}([\"]){1}/, 'gm'); // filter for smartifying [5"] with [5“] | |
var _d = new RegExp(/([^\s0-9]{1,}[\'][a-zA-Z]{1,})/, 'gm'); // filter for getting words like [don't] and [couldn't] | |
var _e = new RegExp(/([\']([a-zA-Z0-9\s\S]{0,}?)([\']))/, 'gm'); // gets large chunks of dumb apostrophes like ['What did she say?'] and ['thus we heard it'] | |
var _f = new RegExp(/([^\s0-9]{0,}[\'][a-zA-Z]{0,})/, 'gm'); // gets the remaining dumb apostrophes like ['bout] and [lyin'] | |
var _g = new RegExp(/(\s[\-]{1,2}\s)|((\s){0,1}[\-]{2}(\s){0,1})|((\s){0,1}[\-]\s)|(\s[\-](\s){0,1})/, 'gm'); // gets all the makeshift em dashes and converts them to REAL em dashes | |
var _h = new RegExp(/[\"]/, 'gm'); // matches all dumb quotes | |
var _i = new RegExp(/[\']/, 'gm'); // matches all dumb apostrophes | |
var _j = new RegExp(/[\-]/, 'gm'); // gets all simple dashes | |
var mod_string = | |
base.replace(_c, function (a) { | |
var b = a.replace(_h, ""); | |
return `${b}“`; // converts shorthand inches measurement [6"] to use a smart quote | |
}) .replace(_a, function (a) { | |
var b = a.replace(_h, ""); | |
return `“${b}”`; // converts quotations to use smart quotes | |
}) .replace(_d, function (a) { | |
var b = a.replace(_i, "’"); | |
return b; // converts words like [don't] to use a smart quote | |
}) .replace(_f, function (a) { | |
var b = a.replace(_i, "’"); | |
return `${b}`; | |
}) .replace(_e, function (a) { | |
var b = a.replace(_i, ""); | |
return `‘${b}’`; // converts apostrophes to use | |
}) .replace(_g, " — ") // em dash conversion | |
.replace(_j, "–"); // replaces simple dashes with long conjunctive dashes | |
return mod_string; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Sample paragraph: