简体   繁体   English

如何使用jQuery从元素的文本中删除特定的字符串?

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

I want to remove 12:00:00 AM from following string. 我想从以下字符串中删除12:00:00 AM

<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 2016年1月11日-2016年1月13日

How can I remove just the 12:00:00 AM from complete string all occurrences of it. 如何从完整的字符串中仅删除12:00:00 AM

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

You could use the .replace() method : 您可以使用.replace()方法

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. 如果只想替换第一个匹配项,只需删除g标志。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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