0

I'm trying to use Google Script pull any number of URLs from a chunk of text in a Google spreadsheet cell.

My current script seems to log the same URL twice despite there being additional URLs. The same regex seems to work fine in the regex101 validator. The script I'm using is below and the logged response on the same content seen in the regex101 example is [http://dianeravitch.net/, dianeravitch.net/].

 function findURL() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheets()[1];
  var range = sheet.getRange(4,3);
  var entry = range.getValue();
  var regExp = new RegExp("https?:\/\/([^\"]*)", "gmi"); 
var urlsFound = regExp.exec(entry);
Logger.log(urlsFound); 
}

1 Answer 1

1

It isn't capturing twice, the second value of the array is missing the HTTP, which is outside the the capturing group, the first result in the array is the whole matched value, while the second is the first (and only in this case) capturing group value.

To get all values you need to keep executing the regex until the end of string, as so:

var regExp = new RegExp("https?:\/\/([^\"]*)", "gmi"), urlsFound; 
while( urlsFound = regExp.exec(entry) ) Logger.log( urlsFound[1] );
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.