1

I'm look for an effective way to remove unwanted space in a string. Right down to the point, this is the string from HTML i'm dealing with:

<h1 class="notranslate" data-se="item-name">
                Evil Skeptic 
            </h1>

It appears on this page. So if you run this in console:

alert(document.getElementsByClassName('notranslate')[1].innerHTML);

it would alert a strange looking text, which is unwanted. Is there an effective way to remove this extra spacing?

Alex
  • 1,035
  • 3
  • 16
  • 31

1 Answers1

1

Depending on the browser/version, String.prototype might have a trim method you can use right out of the box:

str.trim();

Otherwise, if you're using jQuery, it has a trim method (and other libraries do as well):

$.trim(str);

Finally, if neither of those options works, you could implement one yourself. Try something like this:

function trim(str) {
  return str.replace(/^\s+/, '').replace(/\s+$/, '');
}
Dan Tao
  • 125,917
  • 54
  • 300
  • 447
  • Im using jquery, so the `$.trim(str);` works perfectly! – Alex Jun 16 '13 at 02:52
  • @Alex: Prefer `str.trim()` if you’re using `getElementsByClassName`; `getElementsByClassName` support pretty much guarantees `String.prototype.trim`. – Ry- Jun 16 '13 at 02:56
  • @minitech: I'm actually using jquery for my script but I posted that because it's simpler to see and i knew i'de get an answer faster :) – Alex Jun 16 '13 at 02:58
  • @Alex: Fair enough! :D – Ry- Jun 16 '13 at 02:59