简体   繁体   English

为什么我不能使用Google Apps脚本对带空格的字符串执行.split()?

[英]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.

相关问题 如何在谷歌应用程序脚本中用空格分隔符分割字符串 - How to split a string with space delimiter in google apps script 为什么我在谷歌脚本中拆分字符串后无法获得数组长度? - Why can't I get an array length after I split a string in google script? 为什么Google Apps脚本无法在数组中找到字符串 - Why won't Google Apps Script find a string in an array 谷歌应用程序脚本 (V8); 为什么我不能在 onOpen 中使用 object 实例? - Google Apps Script (V8); why can't I use an object instance inside of onOpen? 我可以在 Google Apps 脚本中使用字符串语句过滤矩阵或向量吗? - Can I filter a Matrix or Vector with a string sentence in Google Apps Script? 为什么我不能用字符串中的空格替换元音? - Why I can't substitute vowel with space in a string? 为什么不能拆分字符串然后分别调用? - why can't I split a string and then call each? 如何在 Google Apps 脚本中解决这个问题? - How can I solve this in Google Apps Script? Google Workspace - 创建空间 - Google Apps 脚本 - Google Workspace - Create Space - Google Apps Script Google Apps 脚本 - Gmail - 无法可靠地检测垃圾邮件中的某个字符串 - Google Apps Script - Gmail - Can't detect a certain string reliably in spam emails
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM