gpt4 book ai didi

javascript - 使用正则表达式和 javascript 将 anchor href 转换为唯一标签

转载 作者:行者123 更新时间:2023-11-30 15:40:29 25 4
gpt4 key购买 nike

我有这个示例数据:

<products_database>
<product>
<id>##1234
<a name ="toy">toy</a>
<weight>5kg
<a href ="#block">block</a>
</product>
<product>
<id>##56789
<a name ="brick">brick</a>
<a name ="lego">lego</a>
<a name ="block">block</a>
<weight>2kg
<a href ="#toy">toy</a>
</product>
<product>
<id>##1357902
<a href ="#toy">toy</a>
<a href ="#brick">brick</a>
<weight>4kg
</product>
</product_database>

我想将 href 转换成:

<products_database>
<product>
<id>##1234
<a name ="toy">toy</a>
<weight>5kg
<..pd ##56789 #block>block</a>
</product>
<product>
<id>##56789
<a name ="brick">brick</a>
<a name ="lego">lego</a>
<a name ="block">block</a>
<weight>2kg
<..pd ##1234 #toy>toy</a>
</product>
<product>
<id>##1357902
<..pd ##1234 #toy>toy</a>
<..pd ##56789 #brick>brick</a>
<weight>4kg
</product>

href 将被转换成这个独特的标签,它在每个单独的“产品”中获取“id”,其中它们的“a href”值等同于它们的“a name”。我只允许使用 javascript 和正则表达式任何帮助将不胜感激。

最佳答案

我试图弄清楚您到底需要做什么,并编写了一个可能满足您需要的解决方案。

//var findProduct = /<product>\s+<id>(##\d+)\s+((?:<a name ="[^"]+">[^<]+<\/a>)+)\s+<weight>(\d+kg)\s+((?:<a href ="#[^"]+">[^<]+<\/a>)+)\s+<\/product>/g;

var byName = {}, products = [];

var findProduct = /<product>\s+([\W\w]+?)\s+<\/product>/g;
var findTag = /<([\w]+)(?: (name|href) ="#?([^"]+)")?>([^<\n\r]+)/g;

var data = document.getElementById("data").value;
data.replace(findProduct, function(match, tags) {
var product = {
id: "", names: [], weight: "", links: []
};
tags.replace(findTag, function(match, tagName, attr, attrValue, tagValue) {
switch (tagName) {
case "id": product.id = tagValue; break;
case "weight": product.weight = tagValue; break;
case "a":
if (attr === "name") { product.names.push(attrValue); byName[attrValue] = product; }
else /* if (attr === "href") */ { product.links.push(attrValue); }
break;
}
});
products.push(product);
});

data = "<product_database>" + products.map(function(product) {
return "<product><id>" + product.id + "\n<weight>" + product.weight + "\n" +
product.names.map(function(name) {
return "<a name =\"" + name + "\">" + name + "</a>";
}).join("\n") +
product.links.map(function(link) {
return "<..pd " + byName[link].id + " #" + link + ">block</a>";
}).join("\n")
+ "\n</product>";
}).join("\n") + "</product_database>";

document.getElementById("data").value = data;
  <textarea id="data" cols=50 rows=30><products_database>
<product>
<id>##1234
<a name ="toy">toy</a>
<weight>5kg
<a href ="#block">block</a>
</product>
<product>
<id>##56789
<a name ="brick">brick</a>
<a name ="lego">lego</a>
<a name ="block">block</a>
<weight>2kg
<a href ="#toy">toy</a>
</product>
<product>
<id>##1357902
<a href ="#toy">toy</a>
<a href ="#brick">brick</a>
<weight>4kg
</product>
</product_database></textarea>

关于javascript - 使用正则表达式和 javascript 将 anchor href 转换为唯一标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40914246/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com