假设我的数据库中有字符串,如下所示:
Detail
如何通过 Javascript ?
在这些字符串中仅获取网址部分输出应为:
<a class=' text-info' href='description/Q0RFU0NJRDM=/Acetaminophen (Tylenol) Poisoning'/>Acetaminophen (Tylenol) Poisoning<a />
<a class=' text-info' href='description/Q0RFU0NJRDQ=/Achalasia'/>Achalasia<a />
答案 0 :(得分:1)
这是您的答案,它只会为您提供锚标记HTMl
中的网址
var data = '<a class="text-info" href="description/Q0RFU0NJRDM=/Acetaminophen (Tylenol) Poisoning"/>Acetaminophen (Tylenol) Poisoning<a />';
var subString = 'href="';
var endString = '>';
var index =1;
function substringcheck(data,subString,endString,index){
var urlstart = data.split(subString, index).join(subString).length;
var urlend = data.split(endString, index).join(endString).length;
var res = data.substring(urlstart+6,urlend-6);
return res;
}
var result = substringcheck(data, subString,endString, index);
console.log(result);
答案 1 :(得分:1)
您可以按如下方式获取网址: 首先,将锚存储在数组中
urls = ["<a class=' text-info' href='description/Q0RFU0NJRDM=/Acetaminophen (Tylenol) Poisoning'/>Acetaminophen (Tylenol) Poisoning<a />",
"<a class=' text-info' href='description/Q0RFU0NJRDQ=/Achalasia'/>Achalasia<a />"];
现在,使用数组的map方法
var _urls = urls.map(function(url){
var div = document.createElement('div');
div.innerHTML = url;
return div.children[0].getAttribute('href');
});
您可以遍历数组并创建临时div并将锚点添加为innerHTML以获取HTML元素并返回元素的href属性。
<强>段强>
urls = ["<a class=' text-info' href='description/Q0RFU0NJRDM=/Acetaminophen (Tylenol) Poisoning'/>Acetaminophen (Tylenol) Poisoning<a />",
"<a class=' text-info' href='description/Q0RFU0NJRDQ=/Achalasia'/>Achalasia<a />"];
var _urls = urls.map(function(url){
var div = document.createElement('div');
div.innerHTML = url;
return div.children[0].getAttribute('href');
});
console.log(_urls);
答案 2 :(得分:0)
如果这些是在HTML页面中:
var aList = document.querySelectorAll("a");
var aArray = Array.prototype.slice(aList, 0);
aArray.forEach(function(aElement){
console.log(aElement.getAttribute('href'));
});
哦,你没有HTML页面中的那些。所以你基本上可以进行字符串操作:
databaseResults.forEach(function(databaseResult){
var hrefValue = databaseResult.split(" ").filter(function(item){ return item.startsWith("href"); }).split("=")
return hrefValue.substring(1, hrefValue.length - 1);
});
答案 3 :(得分:0)
只需使用一个简单的正则表达式:
var theString = "<a class=' text-info' href='description/Q0RFU0NJRDM=/Acetaminophen (Tylenol) Poisoning'/>Acetaminophen (Tylenol) Poisoning<a />";
var exp = /href='(.+)'/g;
var match = exp.exec(theString);
console.log(match[0]); //outputs: description/Q0RFU0NJRDM=/Acetaminophen (Tylenol) Poisoning
答案 4 :(得分:0)
你可以这样做:
这里coll
是DOM元素的非实时集合,是一个可以迭代的结构数组。
var coll = document.querySelectorAll('.text-info');
console.log(coll.length);
for(var i=0;i<coll.length;i++) {
console.log(coll[i].getAttribute('href'));
}
<a class=' text-info' href='description/Q0RFU0NJRDM=/Acetaminophen (Tylenol) Poisoning'/>Acetaminophen (Tylenol) Poisoning<a />
<a class=' text-info' href='description/Q0RFU0NJRDQ=/Achalasia'/>Achalasia<a />
答案 5 :(得分:0)
从问题来看,输入是a
或dom元素的字符串并不是很清楚:
如果它们是字符串,我们可以使用简单的RegEx:/href=(["|'])(.*)\1/
,
这将在'
之后捕获"
或href=
之间的任何内容。
var strings = [
`<a class=' text-info' href='description/Q0RFU0NJRDM=/Acetaminophen (Tylenol) Poisoning'/>Acetaminophen (Tylenol) Poisoning<a />`,
`<a class=' text-info' href='description/Q0RFU0NJRDQ=/Achalasia'/>Achalasia<a />`
]
var urls = strings.map(s=>{
return /href=(["|'])(.*)\1/.exec(s)[2];
})
console.log(urls)