简体   繁体   中英

How remove a specific string from an element's text using jQuery?

I want to remove 12:00:00 AM from following string.

<div class="modal-body">
    <p>1/11/2016 12:00:00 AM - 1/13/2016 12:00:00 AM</p>
</div>

The results should be:

1/11/2016 - 1/13/2016

How can I remove just the 12:00:00 AM from complete string all occurrences of it.

$('.modal-body > p').text().remove("12:00:00 AM");

You could use the .replace() method :

Example Here

$('.modal-body > p').text(function () {
    return $(this).text().replace(/12:00:00 AM/g, '');
});

Result:

<div class="modal-body">
    <p>1/11/2016  - 1/13/2016</p>
</div>

If you only want to replace the first occurrence, just remove the g flag.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM