diff --git a/IPython/html/static/notebook/js/textcell.js b/IPython/html/static/notebook/js/textcell.js
index ec0b534f9..4264ebf35 100644
--- a/IPython/html/static/notebook/js/textcell.js
+++ b/IPython/html/static/notebook/js/textcell.js
@@ -418,7 +418,7 @@ define([
html = security.sanitize_html(html);
var h = $($.parseHTML(html));
// add id and linkback anchor
- var hash = h.text().replace(/ /g, '-');
+ var hash = h.text().trim().replace(/ /g, '-');
h.attr('id', hash);
h.append(
$('')