1

I wrote a little function which basically says to check if some text is exactly this or that:

function doSelect(text) {
  return $wikiDOM.find(".infobox th").filter(function() {
    return $(this).text() === text;
  });
}

The I run this in order to get the last string after /wiki/ of some links like <a href="/wiki/Geum_River" title="Geum River">Geum River</a>, <a href="/wiki/Korea" title="Korea">Korea</a>

  let pattern = new RegExp('^\/wiki\/');
  doSelect("Location").siblings('td').find('a').map(function(i, el) {
    return console.log(el.href.replace(pattern, ''));
  });

But when I check in console I get the whole jsFiddle link

https://fiddle.jshell.net/wiki/Geum_River
https://fiddle.jshell.net/wiki/Korea

I'd expect

Geum_River
Korea
rob.m
  • 9,843
  • 19
  • 73
  • 162

2 Answers2

1

The issue is because you're using the href attribute of the <a>. By doing this you retrieve the full absolute path of the URI, ie: https://fiddle.jshell.net/wiki/Geum_River instead of the relative path: /wiki/Geum_River

To change this behavior, use the attribute value directly, using attr():

let pattern = new RegExp('^\/wiki\/');
doSelect("Location").siblings('td').find('a').map(function(i, el) {    
  var result = $(this).attr('href').replace(pattern, '');
  console.log(result);
  return result;
});

Also double check that you're using map() correctly. It's intention is to create an array, but you're not doing anything with it's response. It would be worth using each() instead without the return if all you want to do is loop through the selected elements:

let pattern = new RegExp('^\/wiki\/');
doSelect("Location").siblings('td').find('a').each(function(i, el) {    
  var result = $(this).attr('href').replace(pattern, '');
  console.log(result);
});
Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
1

You can also try like this:

//let pattern = new RegExp('^\/wiki\/');
doSelect("Location").siblings('td').find('a').each(function(i, el) {    
  var name = $(this).attr('href').split('wiki/')[1].trim();
  console.log(result);
});
Chandra Kumar
  • 4,127
  • 1
  • 17
  • 25