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">
    [email protected]    </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%[email protected]%20%20%20%20">
    [email protected]    </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.

1

4 Answers 4

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());
3
  • 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, 2008 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 Dec 23, 2008 at 13:03
  • 6
    One thing to be wary of is that IE doesn't consider non-breaking spaces (&nbsp;, &#160;, &#xA0;, \xA0, \u00A0, etc...) as white-space, so /[\s\xA0]+/g might be more reliable for replacing all white-space.
    – travis
    Jan 27, 2010 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.

0
69

Actually, jQuery has a built in trim function:

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

See here for details.

1
  • 3
    because every built in feature must have a jQuery wrapper
    – user2486953
    Jan 13, 2016 at 4:34
25

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

6
  • 1
    This removes leading and trailing white space like the PHP trim function.
    – Paul
    Mar 29, 2012 at 18:21
  • 1
    can you explain yourself ? Feb 24, 2014 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. Aug 21, 2016 at 18:15
  • 5
    How can anything that looks like this /^\s+|\s+$/g be called "regular"?
    – RyanNerd
    Nov 7, 2016 at 3: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. Nov 9, 2016 at 20:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.