0

I'm scraping www.tosdr.org for some data. For example I'm trying to capture the content of the <title> tag as follows:

$.get('https://tosdr.org/', function(response) {
  var body = response.match(/<title>\[ ([\d]*?) \]<\/title>/);
  console.log(body);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

Problem with this is that I'm getting this error:

TypeError: response.match(...) is null

I shouldn't be getting this error. What am I doing wrong ?

Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
mr_incredible
  • 837
  • 2
  • 8
  • 21
  • 1
    The code in your question just shows `null`, not an error. That being said, [don't use Regex to parse HTML](https://stackoverflow.com/a/1732454/519413). Use a HTML parser, then retrieve the data you want from the DOM that's generated – Rory McCrossan Sep 30 '19 at 13:29
  • Well "null" isn't in the title of the site so I still consider it as an error. – mr_incredible Sep 30 '19 at 13:30
  • To get the text try something like `$(response).find('title').text()`, however if you're getting an error that's not going to work. We need to see the actual code which creates the error in order to help you debug it – Rory McCrossan Sep 30 '19 at 13:32
  • You regex would should be `.match(/]*>([^<]+)<\/title>/)[1]` – Carsten Løvbo Andersen Sep 30 '19 at 13:33
  • @Carsten That worked. Apparently I was using old regex-matching rule. Submit it as an answer and I'm gonna tick it for you. – mr_incredible Sep 30 '19 at 13:39

0 Answers0