简体   繁体   English

在javascript中解析这个正则表达式我做错了什么?

[英]What am I doing wrong in parsing this regular expression in javascript?

My string is: 我的字符串是:

<div> (blah blah blah) ---> quite big HTML before coming to this line.<b>Train No. &amp; Name : </b></td><td style="border-bottom:1px solid #ccc;font:12px arial"><span>12672 / SOUTH TRUNK EXP</span></td>

I managed to formulate a regular expression 我设法制定了一个正则表达式

var trainDetails = new RegExp("<b>Train No. &amp; Name : </b></td><td.*>([0-9][a-z][A-Z]+)</span></td>", "m");

But trainDetails are null or are empty. trainDetails为null或为空。

All I am trying to do is to get the train name and the train number within the span element. 我所要做的就是在span元素中获取列车名称和列车编号。

Any pointers where I am doing wrong ? 我做错了什么指针?

It worked for me: 它对我有用:

Using RegExp 使用RegExp

string = '<div> (blah blah blah) ---> quite big HTML before coming to this line.<b>Train No. &amp; 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');

Using DOM 使用DOM

string = ('<b>Train No. &amp; 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;

Assume condition that last "<td>" in string has train detail. 假设字符串中的最后一个“<td>”具有训练细节的条件。

Regular expression is not the ideal solution for this use-case. 正则表达式不是此用例的理想解决方案。 I suggest using your browser's builtin HTML parser to get the inner HTML of the <span> . 我建议使用浏览器的内置HTML解析器来获取<span>的内部HTML。

var el = document.createElement('html');
el.innerHTML = '<div> (blah blah blah) ---> quite big HTML before coming to this line.<b>Train No. &amp; Name : </b></td><td style="border-bottom:1px solid #ccc;font:12px arial"><span>12672 / SOUTH TRUNK EXP</span></td>';
var output = el.getElementsByTagName('span')[0].innerHTML;

The value of the output variable becomes: 输出变量的值变为:

12672 / SOUTH TRUNK EXP

Edit 编辑

If you are interested in a specific <span> , I suggest adding a class to its tag or its parent <td> tag, eg: 如果您对特定的<span>感兴趣,我建议在其标签或其父<td>标签中添加一个类,例如:

<span class="train-number-and-name">
   12672 / SOUTH TRUNK EXP
</span>

And fetch it like this: 并按如下方式获取:

var output = el.querySelector('span.train-number-and-name').innerHTML;

它应该没问题: .+\\<span>(.+)\\<\\/span>.+抓住组#1你会得到它。

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

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