Rynardt
Rynardt

Reputation: 5557

Google App Script Regex

I am using the following code in Google app script to extract a number.

function getBTC_ZAR_ExchangeRate() {
  var response = UrlFetchApp.fetch("http://coinmill.com/rss/BTC_ZAR.xml")
  var xmlText = response.getContentText();
  //var funded = Xml.parse(htmlText, true);
  var rate = xmlText.match(/BTC =\s(.*?)\sZAR<br/);    
  return rate[1];
}

I get an array with two items as a result. Only the second item in the array is the correct one.

result = {"BTC = 27.45 ZAR<br", "27.45"}

What am I doing wrong, because this cannot be the way it is suppose to work?

Upvotes: 0

Views: 969

Answers (2)

sjsyrek
sjsyrek

Reputation: 193

As Phil Bozak answered, this is how match works. But this is a JavaScript function and has nothing to do otherwise with Google Apps Script specifically.

Upvotes: 0

Phil Bozak
Phil Bozak

Reputation: 2822

This is the expected behavior. See the first example on MDN. The values returned from match are 1. the pattern that you matched (you told it to match that whole thing, so it did; 2. followed by the value(s) from that pattern matched (in your case 27.45).

Upvotes: 2

Related Questions