答案 0 :(得分:1)
也许是这样的:
var str = $('#time').text();
var t = str.match(/[\d:]+(?= PM)/g);
for (var i = 0; i < t.length; i++) {
var match = t[i].split(':');
var th = +match[0] + 12 + ' h';
var tm = ' ' + match[1];
var ft = th + tm;
var str = str.replace(t[i], ft).replace(/PM/g, '');
$('#time').html(str);
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id = "time">12:00 AM test text at 9:10 PM this se second 6:00 PM doe not works.</div>
&#13;
正则表达式仅匹配数字(\d
)和冒号后跟PM
(前导空格)。然后将每个匹配分割为:
,并将12添加到第一个分割(以产生24小时时钟)。