I'm using below function to avoid using importxml in Google Sheets:

function importRegex(url, regex_string) {
  var html, content = '';
  var response = UrlFetchApp.fetch(url);
  if (response) {
    html = response.getContentText();
    if (html.length && regex_string.length) {
      var regex = new RegExp( regex_string, "i" );
      content = html.match(regex)[1];
  return content;  

I want to extract text between an HTML tag. The HTML code is like this:

<span class="js-userid-text">
<a href="/5a5zz/">john</a>

I want to extract "john" using class="js-userid-text".

I'm using this forumula in a cell in Google Sheets but it doesn't work:

=importRegex(R2, "<span.*class=.js-userid-text.>(.*)</span>") //R2 contains an URL for the HTML page.
  • why not use directly: =REGEXEXTRACT(R2; "\/"">([A-Za-z]+)\</a") ? – user0 Sep 2 '18 at 9:06
  • @user0 I'm not sure how it solve my problem. I have a sheet with over 50 importxml functions. As there is a limit for using importxml on Google Sheets, I searched the web and found this function to use in Google Apps Script. I just want to extract the text in the <a> element, by referring to its parent element class. – Arash Jalali Sep 2 '18 at 9:36
  • We should give proper attribution, so please add the the link to the source of the script. By the other hand since the URL on R2 isn't included we can't reproduce the problem. Maybe we could still help you but you should provide more details like the troubleshooting steps that you made and the textual errors that you got. – Rubén Sep 9 '18 at 19:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.