简体   繁体   English

正则表达式将文本与首次出现的分隔符进行匹配

[英]RegEx to match text to first occurence of a delimiter

This is the data I want to match with RegEx: 这是我要与RegEx匹配的数据:

<table>
  <tr>
    <td>
      <font size="4">Speciality</font>
    </td>
    <td>
      <font size="4">somespeciality</font>
    </td>
  </tr>
  <tr>
    <td>
      <font size="4">Date</font>
    </td>
    <td>
      <font size="4">somedate</font>
    </td>
  </tr>
</table>

I want to get as a result somespeciality but with this RexEx: 我想结果somespeciality但这个RexEx:

/Speciality[\s\S]*size="4">(.*?)<\/font>/i

I'm getting somedate . 我要去somedate What is the correct way to do this? 正确的方法是什么?

Thanks. 谢谢。

在角色类之后,您需要使用 贪婪量词。

[\s\S]*?

Just for the record, if you did want to do this with plain DOM methods, you'd do something like the following. 仅作记录,如果您确实想使用简单的DOM方法来执行此操作,则可以执行以下操作。 It gets all the elements, finds the first one with text content that matches the text, gets it's tagname, then finds the next element with that tag name and returns the text content: 它获取所有元素,找到第一个具有与文本匹配的文本内容的元素,获取它的标记名,然后找到具有该标记名的下一个元素并返回文本内容:

var data = '<table><tr><td><font size="4">Speciality</font></td>' +
           '<td><font size="4">somespeciality</font></td></tr>' +
           '<tr><td><font size="4">Date</font></td><td><font size="4">' +
           'somedate</font></td></tr></table>';

function getSpecial(text, data) {
  var div = document.createElement('div');
  div.innerHTML = data;
  var tagName;

  var nodes = div.getElementsByTagName('*');

  for (var i=0, iLen=nodes.length; i<iLen; i++) {
    if (tagName && nodes[i].tagName == tagName) {
      return nodes[i].textContent;
    }

    if (nodes[i].textContent.trim() == text) {
      tagName = nodes[i].tagName;
    }
  }
}

console.log(getSpecial('Speciality', data)); // somespeciality

The difficulty with any such approach (including using a regular expression) is that any change to the markup (and resulting DOM) will likely cause the process to fail. 任何此类方法(包括使用正则表达式)的困难在于,对标记(以及由此产生的DOM)的任何更改都可能导致流程失败。

Note that the above requires ES5 and support for textContent , which should be all modern browsers and IE 9+. 请注意,以上要求ES5和对textContent的支持,这应该是所有现代浏览器和IE 9+。 Support for older browsers can be added by adding a polyfill for trim and using nodes[i].textContent || nodes[i].innerText 可以通过添加用于修剪的 nodes[i].textContent || nodes[i].innerText并使用nodes[i].textContent || nodes[i].innerText来添加对旧版浏览器的支持。 nodes[i].textContent || nodes[i].innerText . nodes[i].textContent || nodes[i].innerText The rest will be fine. 其余的会没事的。

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

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