Skip to content

Instantly share code, notes, and snippets.

@epheatt
Created January 29, 2012 04:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save epheatt/1697194 to your computer and use it in GitHub Desktop.
Save epheatt/1697194 to your computer and use it in GitHub Desktop.
jQuery websafe Unicode
$.fn.websafeUnicode = function(options) {
var ival = $(this).val();
var oval = $.trim(ival);
if( oval.length > 0 ){
//strip iso-8859-1 and windows-1252 control characters
oval = oval.replace(/[\u0081\u008D\u008F\u0090\u009D]/g,'');
//Shift iso-8859-1 and windows-1252 up to unicode websafe equivalents
oval = oval.replace(/[\u0080]/g,'\u20AC'); //euro sign
oval = oval.replace(/[\u0082]/g,'\u201A'); //single low-9 quotation mark
oval = oval.replace(/[\u0083]/g,'\u0192'); //florin currency symbol
oval = oval.replace(/[\u0084]/g,'\u201E'); //double low-9 quotation mark
oval = oval.replace(/[\u0085]/g,'\u2026'); //horizontal ellipsis
oval = oval.replace(/[\u0086]/g,'\u2020'); //dagger
oval = oval.replace(/[\u0087]/g,'\u2021'); //double dagger
oval = oval.replace(/[\u0088]/g,'\u02C6'); //modifier letter circumflex accent
oval = oval.replace(/[\u0089]/g,'\u2030'); //per mille sign
oval = oval.replace(/[\u008A]/g,'\u0160'); //latin capital letter s with caron
oval = oval.replace(/[\u008B]/g,'\u2039'); //single left-pointing angle quotation mark
oval = oval.replace(/[\u008C]/g,'\u0152'); //latin capital ligature oe
oval = oval.replace(/[\u008E]/g,'\u017D'); //latin capital letter z with caron
oval = oval.replace(/[\u0091]/g,'\u2018'); //left single quotation mark
oval = oval.replace(/[\u0092]/g,'\u2019'); //right single quotation mark
oval = oval.replace(/[\u0093]/g,'\u201C'); //left double quotation mark
oval = oval.replace(/[\u0094]/g,'\u201D'); //right double quotation mark
oval = oval.replace(/[\u0095]/g,'\u2022'); //bullet
oval = oval.replace(/[\u0096]/g,'\u2013'); //en dash
oval = oval.replace(/[\u0097]/g,'\u2014'); //em dash
oval = oval.replace(/[\u0098]/g,'\u02DC'); //small tilde
oval = oval.replace(/[\u0099]/g,'\u2122'); //trade mark sign
oval = oval.replace(/[\u009A]/g,'\u0161'); //latin small letter s with caron
oval = oval.replace(/[\u009B]/g,'\u203A'); //single right-pointing angle quotation mark
oval = oval.replace(/[\u009C]/g,'\u0153'); //latin small ligature oe
oval = oval.replace(/[\u009E]/g,'\u017E'); //latin small letter z with caron
oval = oval.replace(/[\u009F]/g,'\u0178'); //latin capital letter y with diaeresis
//Replace nonbreaking space with regular space
oval = oval.replace(/[\u00A0]/g,'\u0020');
//Shift common punctiation down to ASCII
//soft hyphen, hyphen, non-breaking-hyphen, figure dash, en dash, em dash, horizontal bar,
//hyphen bullet, small em dash, small hyphen-minus and fullwidth hyphen-minus to hyphen-minus
oval = oval.replace(/[\u00AD\u2010\u2011\u2012\u2013\u2014\u2015\u2043\uFE58\uFE63\uFE0D]/g,'\u002D');
//left, right and high-reversed-9 single quotation mark to apostrophe
oval = oval.replace(/[\u2018\u2019\u201B]/g,'\u0027');
//left, right and high-reversed-9 double quotation mark to quotation mark
oval = oval.replace(/[\u201C\u201D\u201F]/g,'\u0022');
//single and double low-9 quotation mark to comma
oval = oval.replace(/[\u201A\u201E]/g,'\u002C');
$(this).val(oval);
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment