Google Apps脚本 - 使用.replace方法删除空格对我无效 [英] Google Apps Script - remove spaces using .replace method does not work for me

查看:101
本文介绍了Google Apps脚本 - 使用.replace方法删除空格对我无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Google Apps脚本来创建应用程序。
当我尝试从电子表格中删除空格时遇到问题。
我提到了很多帖子和文章。在stackoverflow和其他论坛也有评论。他们都在讨论使用 .replace 方法。
但是, .replace 方法对我无效。

I am using Google Apps Script to create apps. I encounter issue when I try to remove whitespaces from my spreadsheet value. I have referred alot of posts & comments in stackoverflow and other forum too. They are all talking about using .replace method. However, .replace method does not work for me.

var ItemArray = <<getValue from google spreadsheet>>
var tValue = ItemArray[0][2].toString();

for (var row = 0; row<ItemArray.length; row++)
{
   var TrimmedStrA = ItemArray[row][2].toString().replace(' ', '');
   var TrimmedStrB = tValue.replace(' ', '');

   if (TrimmedStrA == TrimmedStrB)
   {
      <<other code>>

   } //end if
} //end of loop


推荐答案

在replace()方法中应该使用一个简单的RegExp对象。 \是找到空白的简单解决方案。 'g'提供了空白实例的全局匹配。

A simple RegExp object should be used in the replace() method. \s is a simple solution to find whitespace. The 'g' provides a global match for instances of whitespace.

t.Value.replace(/\s/g, "") 

这会让你感觉很近,不知道你的数据是什么样的。

This will get you pretty close without knowing what your data looks like.

.replace ()文档。

这篇关于Google Apps脚本 - 使用.replace方法删除空格对我无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆