简体   繁体   English

使用JavaScript正则表达式查找包含特定文本的html注释的父节点

[英]Using JavaScript Regex to find parent node of html comment containing specific text

I'm looping over an array and trying to find the parent html elements (tr & td) of an html comment tag containing specific text matching the current array value. 我正在遍历数组,并尝试查找包含与当前数组值匹配的特定文本的html注释标签的父html元素(tr&td)。

For the example below, I'm passing in the value of myField as dev_LocationID but it is always returning null. 对于下面的示例,我将myField的值作为dev_LocationID传递,但它始终返回null。 In this examle I'm trying to match the full string FieldInternalName = ' dev_LocationID ' I know the text is there. 在此示例中,我尝试匹配完整的字符串FieldInternalName ='dev_LocationID',我知道文本在那里。 What am I doing wrong? 我究竟做错了什么? Any help would be appreciated! 任何帮助,将不胜感激!

myField = 'dev_LocationID';

function fnFindThisField(myField){
    var myFieldInternalName = "FieldInternalName='" +myField+ "'";  
    regexComment = new RegExp(/<!--\s*/myFieldInternalName\s*-->/g);
    targetElement = regexComment.test($('td').html());
    return targetElement;
}

Sample of html html样本

<tr>
  <td><H3 ><nobr>Form Label</nobr></td>
  <td class="ms-formbody">  
     <!-- FieldName='Location ID'   FieldInternalName='dev_LocationID'  -->
    <select name="ctl00$dev_LocationID" id="ctl00_dev_LocationID">
    <option value="Please choose...">Please choose...</option>
    <option selected="selected" value="1">Location 1</option>
    <option selected="selected" value="1">Location 2</option>
  </select>
  </td>
</tr>

You don't take care to match the "FieldName='Location ID'" part in your comment, so your regex does not match. 您无需注意在注释中匹配“ FieldName ='Location ID'”部分,因此正则表达式不匹配。

The other thing is, it is very difficult (sometimes impossible) to handle html with regex, think about using a parser. 另一件事是,使用正则表达式来处理html非常困难(有时是不可能的),请考虑使用解析器。

Try this: 尝试这个:

myField = 'dev_LocationID';

function fnFindThisField(myField){
    var myFieldInternalName = "FieldInternalName='" +myField+ "'";  
    regexComment = new RegExp("/<!--.+?" + myFieldInternalName +"\s*-->"/g);
    targetElement = regexComment.test($('td').html());
    return targetElement;
}

Your code isn't legal JavaScript code. 您的代码不是合法的JavaScript代码。 The string variable that you want to use to build your regular expression isn't interpolated as you might think it would be. 您想用来构建正则表达式的字符串变量没有插值。 Here, I'm just using the RegExp constructor function that accepts a string and manually build the pattern using string concatenation. 在这里,我只是使用RegExp构造函数,该函数接受字符串并使用字符串串联手动构建模式。


Note that parsing HTML using regex is likely to be a very brittle solution. 请注意,使用正则表达式解析HTML可能是一种非常脆弱的解决方案。 Parsing arbitray HTML using Regex isn't even possible, as others on StackOverflow have written about before . 甚至不可能使用Regex解析arbitray HTML, 因为StackOverflow上的其他人之前已经写过

The best way is to actually walk the DOM tree instead of regex-searching its HTML string representation. 最好的方法是实际遍历DOM树,而不是正则表达式搜索其HTML字符串表示形式。

→ jsFiddle →jsFiddle

function searchComments($dom) {
    var comments = [];

    $dom.contents().each(function (idx, node) {
        if (node.nodeType === Node.ELEMENT_NODE) {
            comments = comments.concat(searchComments($(node)));
        } else if (node.nodeType === Node.COMMENT_NODE) {
            comments.push(node);
        }
    });
    return comments;
}

function fnFindThisField(myField) {
    var myFieldInternalName = "FieldInternalName='" + myField + "'";
    var foundCommentNode = null;

    searchComments($(document.body)).every(function (commentNode) {
        if (commentNode.nodeValue.indexOf(myFieldInternalName) !== -1) {
            foundCommentNode = commentNode;
            return false;
        }
        return true;
    });
    return foundCommentNode;
}
console.log(fnFindThisField("dev_LocationID").parentNode);

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

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