0

Suppose I have a String in my DB like this:

<a class=' text-info' href='description/Q0RFU0NJRDM=/Acetaminophen (Tylenol) Poisoning'&#47;>Acetaminophen (Tylenol) Poisoning<a &#47;>

<a class=' text-info' href='description/Q0RFU0NJRDQ=/Achalasia'&#47;>Achalasia<a &#47;>

How can I get only URL part in these strings through Javascript?

The output should be:

description/Q0RFU0NJRDQ=/Achalasia 

description/Q0RFU0NJRDM=/Acetaminophen (Tylenol) Poisoning
Clonkex
  • 3,373
  • 7
  • 38
  • 55
Aman
  • 806
  • 2
  • 12
  • 38

6 Answers6

1

You can get the urls as follows: Firstly, you store the anchors in an array

urls  = ["<a class=' text-info' href='description/Q0RFU0NJRDM=/Acetaminophen (Tylenol) Poisoning'&#47;>Acetaminophen (Tylenol) Poisoning<a &#47;>",
"<a class=' text-info' href='description/Q0RFU0NJRDQ=/Achalasia'&#47;>Achalasia<a &#47;>"];

Now, using map method of array

var _urls = urls.map(function(url){
   var div = document.createElement('div');
   div.innerHTML =  url;
   return div.children[0].getAttribute('href');
});

You can iterate over the array and create a temporary div and add the anchor as innerHTML to get an HTML Element and return the href property of the element.

SNIPPET

urls  = ["<a class=' text-info' href='description/Q0RFU0NJRDM=/Acetaminophen (Tylenol) Poisoning'&#47;>Acetaminophen (Tylenol) Poisoning<a &#47;>",
"<a class=' text-info' href='description/Q0RFU0NJRDQ=/Achalasia'&#47;>Achalasia<a &#47;>"];

var _urls = urls.map(function(url){
   var div = document.createElement('div');
   div.innerHTML =  url;
   return div.children[0].getAttribute('href');
});

console.log(_urls);
Nadir Laskar
  • 4,012
  • 2
  • 16
  • 33
0

If those were in an HTML page:

var aList = document.querySelectorAll("a");
var aArray = Array.prototype.slice(aList, 0);
aArray.forEach(function(aElement){
    console.log(aElement.getAttribute('href'));
});

Oh you don't have those in an HTML page. So you can basically do string manipulation:

databaseResults.forEach(function(databaseResult){
    var hrefValue = databaseResult.split(" ").filter(function(item){ return item.startsWith("href"); }).split("=")
    return hrefValue.substring(1, hrefValue.length - 1);
});
ardilgulez
  • 1,856
  • 18
  • 19
0

Just use a simple regex like so:

var theString = "<a class=' text-info' href='description/Q0RFU0NJRDM=/Acetaminophen (Tylenol) Poisoning'&#47;>Acetaminophen (Tylenol) Poisoning<a &#47;>";
var exp = /href='(.+)'/g;
var match = exp.exec(theString);
console.log(match[0]); //outputs: description/Q0RFU0NJRDM=/Acetaminophen (Tylenol) Poisoning
Clonkex
  • 3,373
  • 7
  • 38
  • 55
0

You can do this: Here coll is non-live collection for DOM elements and is an array like structure over which you can iterate.

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'&#47;>Acetaminophen (Tylenol) Poisoning<a &#47;>

<a class=' text-info' href='description/Q0RFU0NJRDQ=/Achalasia'&#47;>Achalasia<a &#47;>
Pankaj Shukla
  • 2,657
  • 2
  • 11
  • 18
  • He said they were strings in a DB; your answer assumes they are in the DOM. – Clonkex May 23 '17 at 06:15
  • @Clonkex If that is the case, how is your solution going to help? Shouldn't then a query be written instead of `console.log` in your solution? I have asked him the question though! Question is not very clear. – Pankaj Shukla May 23 '17 at 06:21
  • My solution assumes he knows how to get the data out of the DB but not how to extract the substring from the string. My use of `console.log()` is to demonstrate how to use resulting variable, and is quite common. – Clonkex May 23 '17 at 06:24
  • @Clonkex Yeah, assumption! It is entirely possible that he has these tags in DB, reads them on server side and sends back as html to the browser. Possible? – Pankaj Shukla May 23 '17 at 06:26
  • Well he didn't specify. You're _assuming_ he has them in the DOM with your answer. It's normal to assume. It's also all we can do since he didn't tell us one way or the other. Personally I think it's obvious that he means he simply wants to get the `href` part out of a normal string, but if I'm wrong he will correct me. – Clonkex May 23 '17 at 06:27
  • @Clonkex Yeah same with me. Please have patience when you also made an assumption and don't be quick to judge other's solution. – Pankaj Shukla May 23 '17 at 06:29
  • I feel it's fair that I assumed to be correct, when that's what he actually _said_. For your answer to be correct he would have had to leave out fairly important information and mean something other than what he said. But my intention was not to judge, only to point out that I thought you may have mis-read the question, so apologies if I came across as judgmental. – Clonkex May 23 '17 at 06:32
  • 1
    @Clonkex No worries! I also didn't mean that you apologize to me. I just wanted both of us to wait for clarification since we both agree that the problem is poorly structured. By the way it's unusual to the best of my knowledge that one keeps these tags in DB. So my assumption(which I thought was fair) was to think it is in DOM. :) – Pankaj Shukla May 23 '17 at 06:39
0

From the question it's not really clear whether the input is string of a or dom elements:

in case they are strings we can use a simple RegEx: /href=(["|'])(.*)\1/, that will capture anything between ' or " after a href=.

var strings = [
`<a class=' text-info' href='description/Q0RFU0NJRDM=/Acetaminophen (Tylenol) Poisoning'&#47;>Acetaminophen (Tylenol) Poisoning<a &#47;>`,
`<a class=' text-info' href='description/Q0RFU0NJRDQ=/Achalasia'&#47;>Achalasia<a &#47;>`
]

var urls = strings.map(s=>{
  return /href=(["|'])(.*)\1/.exec(s)[2];
})

console.log(urls)
TedMeftah
  • 1,845
  • 1
  • 21
  • 29
0

Here is you answer which will give you only url from your anchor-tag HTMl

var data = '<a class="text-info" href="description/Q0RFU0NJRDM=/Acetaminophen (Tylenol) Poisoning"&#47;>Acetaminophen (Tylenol) Poisoning<a &#47;>';

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);
CyberAbhay
  • 494
  • 6
  • 17