简体   繁体   English

正则表达式提取HTML注释属性

[英]regex to extract html comment attribute

I have a table cell with the following comment: 我有一个带有以下注释的表格单元:

 <!-- FieldName="Predicted Process Equipment Potable Usage"
             FieldInternalName="PredictedProcessEquipmentPotable"
             FieldType="SPFieldNumber" -->

There are many table cells with similar pattern comments, where the values of the attribute differ, but the attributes are always the same (FieldName, FieldInternalName, and FieldType). 有许多具有相似模式注释的表单元格,其中属性的值不同,但是属性始终相同(FieldName,FieldInternalName和FieldType)。

How do I extract using REGEX the FieldInternalname in this type of comment? 如何使用REGEX提取此类注释中的FieldInternalname? Is there a non-regex way of doing it too? 是否也有非正则表达式的方式呢?

Please help! 请帮忙!

Use the below regex and get the value of FieldInternalName from group index 1. 使用下面的正则表达式,并从组索引1获取FieldInternalName的值。

<!--[\S\s]*?FieldInternalName="([^"]*)"[\S\s]*?-->

DEMO 演示

> var m = 'foo\n <!-- FieldName="Predicted Process Equipment Potable Usage"\n            FieldInternalName="PredictedProcessEquipmentPotable"\n           FieldType="SPFieldNumber" -->';
undefined
> console.log(/<!--[\S\s]*?FieldInternalName="([^"]*)"[\S\s]*?-->/.exec(m)[1]);
PredictedProcessEquipmentPotable

Is there a non-regex way of doing it too? 是否也有非正则表达式的方式呢?

commentNode.data;

Comment nodes have nodeType of 8 , so if you don't know which child of the cell it will be, you can write a function like this Comment节点的nodeType8 ,因此,如果您不知道它将是单元格的哪个子代,则可以编写如下函数

function getNodesByType(node, type, childrenOnly) {
    var i,
        o = [];
    if (node.childNodes && node.childNodes.length)
        for (i = 0; i < node.childNodes.length; ++i)
            if (node.childNodes[i].nodeType === type)
                o.push(node.childNodes[i]);
            else if (!childrenOnly && node.childNodes[i].nodeType === 1)
                Array.prototype.push.apply(
                    o,
                    getNodesByType(node.childNodes[i], type, childrenOnly)
                );
    return o;
}

And invoke getNodesByType(yourTdElement, 8, true); 并调用getNodesByType(yourTdElement, 8, true); to get an Array of comment nodes which are immediate children of your <td> 获取评论节点数组 ,这些评论节点是您<td>直接子级

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

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