Google App Script Regex

时间:2013-02-27 12:38:21

标签: xml regex google-apps-script

我在Google应用脚本中使用以下代码来提取数字。

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];
}

我得到一个包含两个项目的数组。只有数组中的第二项是正确的。

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

我做错了什么,因为这不可能是它的工作方式?

2 个答案:

答案 0 :(得分:2)

这是预期的行为。 See the first example on MDN。匹配返回的值是1.你匹配的模式(你告诉它匹配整个事物,所以它做了; 2.然后是匹配的模式中的值(在你的情况下是27.45)。

答案 1 :(得分:0)

正如Phil Bozak回答的那样,匹配是如何运作的。但这是一个JavaScript功能,与Google Apps脚本无关。