Skip to content

Instantly share code, notes, and snippets.

@karbassi
Created August 12, 2013 23:49
Show Gist options
  • Star 11 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save karbassi/6216484 to your computer and use it in GitHub Desktop.
Save karbassi/6216484 to your computer and use it in GitHub Desktop.
Really small native javascript function to convert text with straight quotes to curly/smart quotes.
function curlies(element) {
function smarten(text) {
return text
/* opening singles */
.replace(/(^|[-\u2014\s(\["])'/g, "$1\u2018")
/* closing singles & apostrophes */
.replace(/'/g, "\u2019")
/* opening doubles */
.replace(/(^|[-\u2014/\[(\u2018\s])"/g, "$1\u201c")
/* closing doubles */
.replace(/"/g, "\u201d")
/* em-dashes */
.replace(/--/g, "\u2014");
};
var children = element.children;
if (children.length) {
for(var i = 0, l = children.length; i < l; i++) {
curlies(children[i]);
}
} else {
element.innerHTML = smarten(element.innerHTML);
}
};
<!DOCTYPE html>
<head>
<title>curlier - native</title>
</head>
<body>
<section class="what">
<div class="awesome">
<div class="classnames">
<article class="i-have">
<h1>It's a good day.</h1>
<h2>What's up my friend?</h2>
<p>He said, "She's my best friend."</p>
<a href="#">Go "Here", I say.</a>
</article>
</div>
</div>
</section>
<script src="curlies.js"></script>
<script>
curlies( document.body );
</script>
</body>
</html>
@RealLongwayround
Copy link

Same problem here with the [br] element

@loesberg
Copy link

loesberg commented Oct 2, 2017

This just solved a huge problem for me. Thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment