我的字符串是:
<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 元素中获取列车名称和列车编号。
有什么我做错的地方吗?
最佳答案
它对我有用:
使用正则表达式
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;
假设条件是字符串中的最后一个“
关于javascript - 我在用 javascript 解析这个正则表达式时做错了什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34527786/
相关文章:
jquery - 如何在 jquery datetimepicker 中将 24 小时下拉列表更改为 12 小时下拉列表
javascript - 在 React/JavaScript 中不知道字体的情况下计算文本的像素宽度
javascript - 正则表达式:允许除某些选定字符外的所有字符
javascript - jQuery 选择器选择每个字母的第一个元素
javascript - iOS中未调用Youtube IFrame API getVideoLoadedFraction()
javascript - 为什么 SauceLabs 在我的 QUnit 测试明显通过时却说它失败了?
javascript - MongoDB: "Unsupported projection option: pop: { $gt: 0.0 }"(调试中)