187

I'm wanting to use jQuery to wrap a mailto: anchor around an email address, but it's also grabbing the whitepace that the CMS is generating.

Here's the HTML I have to work with, the script as I have it and a copy of the output.

HTML

<div class="field field-type-text field-field-email">
  <div class="field-item">
    name@example.com    </div>
</div>

jQuery JavaScript

$(document).ready(function(){
  $('div.field-field-email .field-item').each(function(){
    var emailAdd = $(this).text();
      $(this).wrapInner('<a href="mailto:' + emailAdd + '"></a>');
   });
 });

Generated HTML

<div class="field field-type-text field-field-email">
  <div class="field-items"><a href="mailto:%0A%20%20%20%20name@example.com%20%20%20%20">
    name@example.com    </a></div>
</div>

Though I suspect that others reading this question might want to just strip the leading and tailing whitespace, I'm quite happy to lose all the whitespace considering it's an email address I'm wrapping.

John
  • 1
  • 13
  • 98
  • 177
Steve Perks
  • 5,490
  • 3
  • 28
  • 41

4 Answers4

339

Use the replace function in js:

var emailAdd = $(this).text().replace(/ /g,'');

That will remove all the spaces

If you want to remove the leading and trailing whitespace only, use the jQuery $.trim method :

var emailAdd = $.trim($(this).text());
Andreas Grech
  • 105,982
  • 98
  • 297
  • 360
  • 11
    Correct but /\s/g would be a clearer pattern (the "i" is redundant and the ' ' is an unusual form - also fwiw a pattern of /(^\s*)|(\s*$)/g is equivalent to trim. – annakata Dec 18 '08 at 10:04
  • 1
    You are correct annakata, I removed the /i because it is redundant since case sensitivity in this case is not an issue – Andreas Grech Dec 23 '08 at 13:03
  • 6
    One thing to be wary of is that IE doesn't consider non-breaking spaces ( ,  ,  , \xA0, \u00A0, etc...) as white-space, so /[\s\xA0]+/g might be more reliable for replacing all white-space. – travis Jan 27 '10 at 17:57
105

Javascript has built in trim:

str.trim()

It doesn't work in IE8. If you have to support older browsers, use Tuxmentat's or Paul's answer.

Jhankar Mahbub
  • 9,746
  • 10
  • 49
  • 52
69

Actually, jQuery has a built in trim function:

 var emailAdd = jQuery.trim($(this).text());

See here for details.

Tuxmentat
  • 1,089
  • 1
  • 9
  • 13
24

str=str.replace(/^\s+|\s+$/g,'');

Community
  • 1
  • 1
Paul
  • 241
  • 2
  • 2
  • 1
    This removes leading and trailing white space like the PHP trim function. – Paul Mar 29 '12 at 18:21
  • 1
    can you explain yourself ? – Francisco Corrales Morales Feb 24 '14 at 15:26
  • 1
    @FranciscoCorralesMorales: I think you have a choice here: Learn about regular expressions or don't. If you do, the expression will become self-evident, and if you don't it will be hard to explain so it is easy to understand. So: Learn about regular expressions. If you still don't understand after trying to learn them, ask a specific question about exactly what you don't understand. – Peter V. Mørch Aug 21 '16 at 18:15
  • 5
    How can anything that looks like this /^\s+|\s+$/g be called "regular"? – RyanNerd Nov 07 '16 at 03:44
  • They are called regular expressions because they can describe regular languages, languages that can be parsed using a finite-state machine. Regular expressions, in their purest form, are in fact a way to describe a finite-state machine. Considering how hard it would be to include a diagram of an automaton in your code in a way that the computer could understand, regular expressions are pretty awesome even if their terseness can make them quite hard to parse for humans. – QuantumOmega Nov 09 '16 at 20:05
  • A humor failure logged. – RichieHH Oct 12 '17 at 12:29