Filip Breckx
Filip Breckx

Reputation: 55

Remove comma at start of string

Campaign Monitor has a script that outputs your campaign archive. This is the output:

<article class="node history">
  <ul>
    <li><a href="http://xxx.createsend.com/t/ViewEmailArchive/x/1448CC2F5A9BABD9/C67FD2F38AC4859C/" class="ext">Title</a>, 04 July 2012</li>
  </ul>
</article>

Because I want to separate the date from the title (by giving the a-element a certain width); how can I get rid of the comma and space that's in front of the date - using jQuery?

When I try this next bit of code, my a-element within the li-element disappears (so no more link, everything is just plain text).

UPDATE: okay, so this works for the comma. How to remove the space as well?

$(".node.history ul li").each(function() {
      $(this).html($(this).html().replace(/[,]/, ""));
});​

Upvotes: 0

Views: 1137

Answers (4)

RichardTowers
RichardTowers

Reputation: 4762

As mentioned in the other answers, you need to fix your selector, and use $().html() instead of $().text().

Your regex also doesn't want the square brackets - these define a set of characters to match (in your case one character that is either a comma or a space when you want [I think] comma followed by space). Full solution:

<ul class="node history"> 
    <li>
        <a href=http://xxx.createsend.com/t/ViewEmailArchive/x/1448CC2F5A9BABD9/C67FD2F38AC4859C/ class="ext">Title</a>, 04 July 2012
    </li>
</ul>
$("ul.node.history li").each(function() {
    var result = $(this).html().replace(/, /, "");
    $(this).html(result);
    alert(result);
});

http://jsfiddle.net/RichardTowers/H5Hcj/

Upvotes: 1

Jorge Loureiro
Jorge Loureiro

Reputation: 458

If I understand your question, try simple change your html string to:

<article class="node history">
  <ul>
    <li><a href="http://xxx.createsend.com/t/ViewEmailArchive/x/1448CC2F5A9BABD9/C67FD2F38AC4859C/" class="ext">Title</a>&nbsp 04 July 2012</li>
  </ul>
</article>

Remove comma and add '&nbsp' (white space)

Upvotes: 0

Aravindhan
Aravindhan

Reputation: 3626

this may remove the commas before the character

  $("ul.node.history li").each(function() {
 $(this).text($(this).text().replace(/\,/g, ""));
 });​

Upvotes: 1

Ohad
Ohad

Reputation: 1719

use $.html() instead of $.text()

Upvotes: 1

Related Questions