字符串拆分<href>

时间:2017-06-09 22:41:17

标签: javascript jquery html

我是Jquery.的新手,我需要提取时间并将其附加到标记的末尾。

<html>
5:00 am - 8:am <a href='/sites/ERR/ERR/HC/ERRC Items/DispForm.aspx?ID=4' target="_blank">Implementation of two gateway</a>'
</html>

格式化后,我的字符串应如下所示::

'<a href='/sites/ERR/ERR/HC/ERRC Items/DispForm.aspx?ID=4' target='_blank">Implementation of two gateway</a> 5:00 am- 8: am '
你能帮忙吗?

1 个答案:

答案 0 :(得分:-1)

您可以使用简单的正则表达式查找时间并调整字符串:

// INIT TIME STRING
var timeString = "5:00 am - 5: am Implementation of two gateway";

// INIT REGEX
var regex = /\d:\d?\d?\s?(am||pm)?\s?-?\s?\d:\d?\d?\s?(am||pm)?/g;

// PRINT BEFORE
console.log("BEFORE: "+timeString);

// RUN REGEX
match = regex.exec(timeString);

// CHECK IF TIME FOUND
if (match != null) {
  
  // REMOVE TIME VALUE
  timeString = timeString.replace(match[0], "");

  // ADD TIME TO END
  timeString = timeString+" "+match[0];

}

// PRINT AFTER
console.log("AFTER: "+timeString);