Cloudant 中的 JS trim() 函数
JS trim() function in cloudant
我已经在 Cloudant 中构建了一个搜索索引。我使用 trim()
删除字符串中的 space。但是,它不起作用。
我该怎么做?
更新:
我有一个JSON对象
...
"attributeArray": [
{
"name": "this is a web authentication"
}
}
...
我已经成功提取"name"。我想删除 "name" 中的 space 然后为该文档创建一个搜索索引。假设 "name" 已经被提取出来。
var index=name.trim();
Index("default", index);
我查询时,系统显示:
{
"id": "06xxxx",
"fields": [
" this is a web authentication"
]
}
我断定 trim()
函数不起作用。
PS: 一个小问题,不用一一解释。
根据定义,trim()
函数只会删除字符串中的前导和尾随白色-space,这在这种情况下可能不是您需要的。
如果您想要删除所有白色-space,您可以考虑通过 replace()
函数使用正则表达式替换:
// This will remove all white-space from your input string
var output = input.replace(/\s/g,'');
更新后
您的代码看起来更像是您想用单个 space 替换 space 的多个实例,这仍然可以通过与原始表达式略有不同的表达式来完成:
// This replaces multiple repeated instances of a space with a single
var trimmedName = name.replace(/\s+/g,' ');
Index("default", trimmedName);
trim() 函数只会删除字符串的前导和尾随白色-space,不会删除字符串单词
之间的spaces
您确定在 trim()
之后重新分配您的变量吗?
var test = " Hello World ";
test = test.trim(); // "Hello World"
我已经在 Cloudant 中构建了一个搜索索引。我使用 trim()
删除字符串中的 space。但是,它不起作用。
我该怎么做?
更新:
我有一个JSON对象
...
"attributeArray": [
{
"name": "this is a web authentication"
}
}
...
我已经成功提取"name"。我想删除 "name" 中的 space 然后为该文档创建一个搜索索引。假设 "name" 已经被提取出来。
var index=name.trim();
Index("default", index);
我查询时,系统显示:
{
"id": "06xxxx",
"fields": [
" this is a web authentication"
]
}
我断定 trim()
函数不起作用。
PS: 一个小问题,不用一一解释。
根据定义,trim()
函数只会删除字符串中的前导和尾随白色-space,这在这种情况下可能不是您需要的。
如果您想要删除所有白色-space,您可以考虑通过 replace()
函数使用正则表达式替换:
// This will remove all white-space from your input string
var output = input.replace(/\s/g,'');
更新后
您的代码看起来更像是您想用单个 space 替换 space 的多个实例,这仍然可以通过与原始表达式略有不同的表达式来完成:
// This replaces multiple repeated instances of a space with a single
var trimmedName = name.replace(/\s+/g,' ');
Index("default", trimmedName);
trim() 函数只会删除字符串的前导和尾随白色-space,不会删除字符串单词
之间的spaces您确定在 trim()
之后重新分配您的变量吗?
var test = " Hello World ";
test = test.trim(); // "Hello World"