       leadingHtml = ''
       trailingHtml = ''
 
-      if hasLeadingWhitespace and match = /^[ ]+/.exec(html)
-        classes = []
-        classes.push('indent-guide') if hasIndentGuide
-        classes.push('invisible-character') if invisibles.space
-        classes.push('leading-whitespace')
-        classes = classes.join(' ')
+      if hasLeadingWhitespace and match = LEADING_WHITESPACE_REGEX.exec(html)
+        classes = 'leading-whitespace'
+        classes += ' indent-guide' if hasIndentGuide
+        classes += ' invisible-character' if invisibles.space
 
         match[0] = match[0].replace(/./g, invisibles.space) if invisibles.space
         leadingHtml = "<span class='#{classes}'>#{@wrapCharacters(match[0])}</span>"
 
         startIndex = match[0].length
 
-      if hasTrailingWhitespace and match = /[ ]+$/.exec(html)
-        classes = []
-        classes.push('indent-guide') if hasIndentGuide and not hasLeadingWhitespace
-        classes.push('invisible-character') if invisibles.space
-        classes.push('trailing-whitespace')
-        classes = classes.join(' ')
+      if hasTrailingWhitespace and match = TRAILING_WHITESPACE_REGEX.exec(html)
+        classes = 'trailing-whitespace'
+        classes += ' indent-guide' if hasIndentGuide and not hasLeadingWhitespace
+        classes += ' invisible-character' if invisibles.space
 
         match[0] = match[0].replace(/./g, invisibles.space) if invisibles.space
         trailingHtml = "<span class='#{classes}'>#{@wrapCharacters(match[0])}</span>"

    ret = ''

    for i in [startIndex...endIndex]
      character = str[i]
        .replace('&', '&amp;')
        .replace('"', '&quot;')
        .replace("'", '&#39;')
        .replace('<', '&lt;')
        .replace('>', '&gt;')
      ret += "<span class='character'>#{character}</span>"

    ret


    html = @value

    if @isHardTab
      classes = 'hard-tab'
      classes += ' indent-guide' if hasIndentGuide
      classes += ' invisible-character' if invisibles.tab
      html = html.replace /^./, (match) =>
        match = invisibles.tab ? match
        "<span class='#{classes}'>#{@wrapCharacters(match)}</span>"
