3 Replies - 1070 Views - Last Post: 21 July 2015 - 04:54 AM

#1 jch053   User is offline

  • New D.I.C Head
  • member icon

Reputation: 7
  • View blog
  • Posts: 34
  • Joined: 20-December 14

Delete everything after a character using jQuery

Posted 06 July 2015 - 11:58 AM

What is the best way to delete all characters after a given one?

For example if I have

<span class=lwn0>
    Jul 6 - Sep 6
</span>

<span class=lwn0>
    Jul 7 - Sep 7
</span>



But I only want Jul 6 and Jul 7. How do I get rid of the " - Sep 6" and " - Sep 7" using jQuery?
Is This A Good Question/Topic? 0
  • +

Replies To: Delete everything after a character using jQuery

#2 modi123_1   User is offline

  • Suitor #2
  • member icon



Reputation: 16479
  • View blog
  • Posts: 65,313
  • Joined: 12-June 08

Re: Delete everything after a character using jQuery

Posted 06 July 2015 - 12:19 PM

It would seem like basic string manipulation. Get the index of the - and make a substring starting from to the index of -.
Was This Post Helpful? 0
  • +
  • -

#3 ArtificialSoldier   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3134
  • View blog
  • Posts: 8,931
  • Joined: 15-January 14

Re: Delete everything after a character using jQuery

Posted 06 July 2015 - 01:20 PM

Here's the string object if you need a reference:

https://developer.mo..._Objects/String
Was This Post Helpful? 0
  • +
  • -

#4 sshfs   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 21-July 15

Re: Delete everything after a character using jQuery

Posted 21 July 2015 - 04:54 AM

You can do so either by using string replace or substring as below.

$(".lwn0").text(function() {
    return $(this).text().replace(/-.+$/m, '');
});

$(".lwn1").text(function() {
    var txt = $(this).text();
    return txt.substring(0, txt.indexOf("-"));
});



Take a look at the demo to see it working.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1