我的字符串是:
<div> (blah blah blah) ---> quite big HTML before coming to this line.<b>Train No. & Name : </b></td><td style="border-bottom:1px solid #ccc;font:12px arial"><span>12672 / SOUTH TRUNK EXP</span></td>
我设法制定了一个正则表达式
var trainDetails = new RegExp("<b>Train No. & Name : </b></td><td.*>([0-9][a-z][A-Z]+)</span></td>", "m");
但是
trainDetails
为null或为空。我要做的就是在span元素内获取火车名称和火车编号。
我在哪里做错任何指针?
最佳答案
它为我工作:
使用RegExp
string = '<div> (blah blah blah) ---> quite big HTML before coming to this line.<b>Train No. & Name : </b></td><td style="border-bottom:1px solid #ccc;font:12px arial"><span>12672 / SOUTH TRUNK EXP</span></td>';
var trainDetail = string.replace( new RegExp(".*?([^\>]+)(?:\<\/[A-z]+\>)+$","g"), '$1');
使用DOM
string = ('<b>Train No. & Name : </b></td><td style="border-bottom:1px solid #ccc;font:12px arial"><span>12672 / SOUTH TRUNK EXP</span></td>');
string = string.replace(new RegExp( '(<\/?)td', 'g'), '$1xmltd');
tempDoc = document.createElement('xml');
tempDoc.innerHTML = string;
node = tempDoc.getElementsByTagName('xmltd');
trainDetails = node[node.length-1].textContent;
假设字符串中最后一个“
09-07 19:08