[英]Why can't I do a .split() on a string with a space in it with Google Apps Script?
I'm having a hard time getting the .split()
function to work when the string by which it's being split has spaces or any characters other than simple letters or numbers in it. 当要分割的字符串中包含空格或除简单字母或数字以外的任何字符时,我很难使
.split()
函数起作用。 I've tried escaping them with backslash notation, but it's not working. 我尝试用反斜杠表示法将它们转义,但是它不起作用。 The code below doesn't supply a second item in the resulting array.
下面的代码没有在结果数组中提供第二项。
function isGoodSerp(kw, optResults, optTld, optStart) {
errorOccurred = false;
kw = kw || "office 365";
optResults = optResults || 10;
optStart = optStart || 0;
optTld = optTld || '.com';
try {
var url = 'http://www.google' + optTld + '/search?q=' + kw + '&start=' + optStart + '&num=' + optResults;
var fullHtml = UrlFetchApp.fetch(url).getContentText();
var noHeaderHtml = fullHtml.split("Search Results");
Logger.log("noHeaderHtml" + noHeaderHtml[1]);
} catch(e) {
errorOccurred = true;
return e;
}
}
I think, the text you want to split with does not exists in the resulting page (as Babajide Fowotade already stated). 我认为,要分割的文本在结果页面中不存在(正如巴巴吉德·福沃塔德(Babajide Fowotade)所述)。 Thus the result is a single element with the complete
fullHtml
-content. 因此,结果是具有完整
fullHtml
的单个元素。
Example: 例:
console.log(("test").split('XXX'));
results in ["test"]
. 结果为
["test"]
。
I don't know what you are trying to reach. 我不知道您要达到什么目标。 There is no text "Search results" in the google search results page - at least not if I use it...
google搜索结果页面中没有文本“搜索结果”-至少如果我使用它,则不会...
Split is working by searching for the text you provide and return the text "around". 拆分通过搜索您提供的文本并返回文本“ around”来工作。
Example: 例:
console.log(("this is a test").split(' '));
results in ["this", "is", "a", "test"]
. 结果为
["this", "is", "a", "test"]
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.