为了方便起见,我自定义了 hreflang 解析器。我想添加一个计数器来显示每个网站的语言总数。我不精通这方面,不能给你更多细节。请参阅下面的代码...
function extractHreflang(url) {
var regex = /<link.*?>/g;
var hreflangReg = /hreflang="(.*?)"/gi;
var hrefReg = /href="(.*?)"/gi;
var data = UrlFetchApp.fetch(url, { 'muteHttpExceptions': true, 'followRedirects': true }).getContentText();
var result = data.match(regex);
var results = [];
var count = [];
if(result) {
var n = 0;
if(result[n].match(hreflangReg) < result.length) {
var hreflang = getMatches(result, hreflangReg, 1).toString();
results.push(hreflang);
}
if(results.length > 0) {
return results;
} else {
return 'No hreflang tags found';
}
} else {
return 'Couldn\'t read URL';
}
}
// FUNCTION: Get data from RegEx capture groups
function getMatches(string, regex, index) {
index || (index = 1); // default to the first capturing group
var matches = [];
var match;
while ((match = regex.exec(string))) {
matches.push(match[index]);
}
return matches;
}
我将如何解决这个问题,以便在 C 列中反映出来?
感谢您的时间!对此的任何帮助都会很棒!
答案 0 :(得分:1)
我相信你的目标如下。
getMatches(result, hreflangReg, 1)
数组的长度放到“C”列中。在这种情况下,如何修改您的脚本如下?
var hreflang = getMatches(result, hreflangReg, 1).toString();
results.push(hreflang);
var hreflang = getMatches(result, hreflangReg, 1);
results.push([hreflang.toString(), hreflang.length]);