Iwani Khalid
Iwani Khalid

Reputation: 303

Remove some text in string with jQuery

<td colspan="2" class="ai1ec-time">
    <a class="ai1ec-button ai1ec-calendar-link" href="#">
         Back to Calendar »                
    </a>
    July 12, 2012 @ 08:00 am – July 13, 2012 @ 10:00 pm
</td>​

Above is my HTML markup which display the text below.

Back to Calendar » July 12, 2012 @ 08:00 am – July 13, 2012 @ 10:00 pm ​

I need to remove @ 08:00 am from the starting date/time and @ 10:00 pm from the ending date/time. How can this be done with jQuery? I heard and read of something called Regexp, is that what needs to be used? With jQuery remove()?

I forgot to mention that the 'time' is not fixed. The only thing that's fixed is that the portion that needs to be removed starts with '@' and ends with 'm'

Upvotes: 4

Views: 3655

Answers (3)

FLY
FLY

Reputation: 2459

first make sure you have jQuery running ( jQuery )

Then use something like this:

$(".ai1ec-time").html(function(i,o){
  return o.replace( /\@\s\d\d\:\d\d\s(am|pm)/ig, '' );
});

demo: js bin

Upvotes: 1

d.k
d.k

Reputation: 4460

var $td = $('td.ai1ec-time')
$td.html($td.html().replace(/@ \d{1,2}:\d{2} [ap]m/g, ''))

Upvotes: 1

Sampson
Sampson

Reputation: 268334

$(".ai1ec-time").html(function(i,o){
  return o.replace( /@[0-9:\s]+(am|pm)/ig, '' );
});

Demo: http://jsbin.com/ozuwob/2/edit

Upvotes: 4

Related Questions