正则表达式查找标签 ID 和内容 JavaScript

发布于 2024-09-10 10:35:14 字数 346 浏览 4 评论 0原文

嘿,我正在尝试用 javascript 中的正则表达式做一些非常具体的事情,而我的 regexp-foo 充其量是不稳定的。想知道是否有专业人士可以为我指出正确的方向。所以我有一些文本...

<item id="myid1">myitem1</item>
<item id="myid2">myitem2</item>

...等等

我想将其剥离到一个数组中读取 myid1, myitem1, myid2, myitem2, ....etc

永远不会有嵌套元素,因此不存在递归嵌套问题。有谁能快速解决这个问题吗? 感谢您的帮助!

Hey I'm trying to do something quite specific with regex in javascript and my regexp-foo is shakey at best. Wondered if there were any pros out there who could point me in the right direction. So I have some text...

<item id="myid1">myitem1</item>
<item id="myid2">myitem2</item>

...etc

And I would like to strip it out into an array that reads
myid1, myitem1, myid2, myitem2, ....etc

There will never be nested elements so there is no recursive nesting problem. Anyone able to bash this out quickly?
Thanks for your help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

水波映月 2024-09-17 10:35:14

这是一个正则表达式,它将:

  • 匹配开始和结束标签元素名称
  • 提取 id 属性的值
  • 提取标签的内部 html 内容

注意:
我懒得在这里匹配属性值。它需要用双引号括起来,并且属性名称与其值之间不需要有空格。

<([^\s]+).*?id="([^"]*?)".*?>(.+?)</\1>

在 javascript 中运行正则表达式将如下完成:

search = '<item id="item1">firstItem</item><item id="item2">secondItem</item>';
regex = new RegExp(/<([^\s]+).*?id="([^"]*?)".*?>(.+?)<\/\1>/gi);
matches = search.match(regex);
results = {};
for (i in matches) {
    parts = regex.exec(matches[i]);
    results[parts[2]] = parts[3];
}

最后,results 将是一个如下所示的对象:

{
    "item1": "firstItem",
    "item2": "secondItem"
}

YMMV if the;元素包含嵌套的 HTML。

Here's a regex that will:

  • Match the starting and ending tag element names
  • Extract the value of the id attribute
  • Extract the inner html contents of the tag

Note:
I am being lazy in matching the attribute value here. It needs to be enclosed in double quotes, and there needs to be no spaces between the attribute name and its value.

<([^\s]+).*?id="([^"]*?)".*?>(.+?)</\1>

Running the regex in javascript would be done like so:

search = '<item id="item1">firstItem</item><item id="item2">secondItem</item>';
regex = new RegExp(/<([^\s]+).*?id="([^"]*?)".*?>(.+?)<\/\1>/gi);
matches = search.match(regex);
results = {};
for (i in matches) {
    parts = regex.exec(matches[i]);
    results[parts[2]] = parts[3];
}

At the end of this, results would be an object that looks like:

{
    "item1": "firstItem",
    "item2": "secondItem"
}

YMMV if the <item> elements contain nested HTML.

回眸一遍 2024-09-17 10:35:14

如果有人真的喜欢或需要使用正则表达式通过 id 获取 HTML 标签(如问题主题中的),他可以使用我的代码:

function GetTagByIdUsingRegex(tag,id,html) {
    return new RegExp("<" + tag + "[^>]*id[\\s]?=[\\s]?['\"]" + id + "['\"][\\s\\S]*?<\/" + tag + ">").exec(html);
}

我还制作了一个通过类名获取元素的代码:

function GetTagByClassUsingRegex(tag,cls,html) {
    return new RegExp("<" + tag + "[^>]*class[\\s]?=[\\s]?['\"]" + cls + "[^'\"]*['\"][\\s\\S]*?<\/" + tag + ">").exec(html);
}

If someone really like or need to use Regex to get an HTML tag by id (like the in the question subject), he can use my code:

function GetTagByIdUsingRegex(tag,id,html) {
    return new RegExp("<" + tag + "[^>]*id[\\s]?=[\\s]?['\"]" + id + "['\"][\\s\\S]*?<\/" + tag + ">").exec(html);
}

I made also one to get element by class name:

function GetTagByClassUsingRegex(tag,cls,html) {
    return new RegExp("<" + tag + "[^>]*class[\\s]?=[\\s]?['\"]" + cls + "[^'\"]*['\"][\\s\\S]*?<\/" + tag + ">").exec(html);
}
衣神在巴黎 2024-09-17 10:35:14

我总是使用这个网站来构建我的正则表达式:

http://www.pagecolumn.com/tool/ regtest.htm

这是我想出的正则表达式:

(<[^>]+>)([^<]+)(<[^>]+>)

这是页面为我提供的 JavaScript 结果

使用 RegExp 对象:

var str = "<item id="myid1">myitem1</item><item id="myid2">myitem2</item><ssdad<sdasda><>dfsf";
var re = new RegExp("(<[^>]+>)([^<]+)(<[^>]+>)", "g");
var myArray = str.match(re);

使用文字:

var myArray = str.match(/(<[^>]+>)([^<]+)(<[^>]+>)/g)

if ( myArray != null) {
    for ( i = 0; i < myArray.length; i++ ) { 
        var result = "myArray[" + i + "] = " + myArray[i];
    }
}

I always use this site to build my regexes:

http://www.pagecolumn.com/tool/regtest.htm

This is the regex I came up with:

(<[^>]+>)([^<]+)(<[^>]+>)

And this is the result that the page gives me for JavaScript

Using RegExp object:

var str = "<item id="myid1">myitem1</item><item id="myid2">myitem2</item><ssdad<sdasda><>dfsf";
var re = new RegExp("(<[^>]+>)([^<]+)(<[^>]+>)", "g");
var myArray = str.match(re);

Using literal:

var myArray = str.match(/(<[^>]+>)([^<]+)(<[^>]+>)/g)

if ( myArray != null) {
    for ( i = 0; i < myArray.length; i++ ) { 
        var result = "myArray[" + i + "] = " + myArray[i];
    }
}
动听の歌 2024-09-17 10:35:14

这是一个 xml 字符串。在我看来,XML 解析器似乎最适合此类任务。执行以下操作:

var items = document.getElementsByTagName("item") ; //<> use the parent element if document is not
var dataArray = [ ] ;

for(var n = 0 ; n < items.length ; n++) {

     var id = items[n].id ;
     var text = items[n].childNodes[0] ;

         dataArray.push(id,text) ;

}

如果您的问题是无法将 xml 字符串转换为 xml 对象,则必须事先使用 DOM 解析器

var xmlString = "" ; //!! your xml string
var document = null ;

    if (window.ActiveXObject) { //!! for internet explorer

            document = new ActiveXObject("Microsoft.XMLDOM") ;
            document.async = "false" ;
            document.loadXML(xmlString) ;

    } else { //!! for everything else

        var parser = new DOMParser() ;
            document = parser.parseFromString(xmlString,"text/xml") ;

    }

然后使用上面的脚本。

This is a xml string. A XML parser seems suited best for this kind of task in my opinion. Do the following:

var items = document.getElementsByTagName("item") ; //<> use the parent element if document is not
var dataArray = [ ] ;

for(var n = 0 ; n < items.length ; n++) {

     var id = items[n].id ;
     var text = items[n].childNodes[0] ;

         dataArray.push(id,text) ;

}

If your problem is that you cannot convert the xml string to an xml object, you will have to use a DOM parser beforehand:

var xmlString = "" ; //!! your xml string
var document = null ;

    if (window.ActiveXObject) { //!! for internet explorer

            document = new ActiveXObject("Microsoft.XMLDOM") ;
            document.async = "false" ;
            document.loadXML(xmlString) ;

    } else { //!! for everything else

        var parser = new DOMParser() ;
            document = parser.parseFromString(xmlString,"text/xml") ;

    }

Then use the above script.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文