[英]JS trim() function in cloudant
I've built a search index in Cloudant.我在 Cloudant 中构建了一个搜索索引。 I use
trim()
to remove space in string.我使用
trim()
删除字符串中的空格。 However, it does not work.但是,它不起作用。
How can I do this?我怎样才能做到这一点?
Update:更新:
I have a JSON object我有一个 JSON 对象
...
"attributeArray": [
{
"name": "this is a web authentication"
}
}
...
I already extracted "name" successfully.我已经成功提取了“名称”。 I want to remove space in "name" then make a search index for the document.
我想删除“名称”中的空格,然后为文档创建一个搜索索引。 Supposing "name" has already been extracted.
假设已经提取了“名称”。
var index=name.trim();
Index("default", index);
When I query, the system shows:当我查询时,系统显示:
{
"id": "06xxxx",
"fields": [
" this is a web authentication"
]
}
I conclude that the function trim()
does not work.我得出结论,函数
trim()
不起作用。
PS: A small question so it does not need to explain in whole thing. PS:一个小问题,所以不需要全部解释。
By definition, the trim()
function will only remove leading and trailing white-space within a string, which may not be what you need in this scenario.根据定义,
trim()
函数只会删除字符串中的前导和尾随空格,这在这种情况下可能不是您所需要的。
If you want to remove all white-space in general, you could consider using a Regular Expression replacement via the replace()
function:如果您想删除所有空白,您可以考虑通过
replace()
函数使用正则表达式替换:
// This will remove all white-space from your input string
var output = input.replace(/\s/g,'');
After Your Update更新后
Your code looks a bit more like you want to replace multiple instances of a space with a single space, which can still be done by a slightly different expression than the original :您的代码看起来更像是您想用一个空格替换一个空格的多个实例,这仍然可以通过与原始表达式略有不同的表达式来完成:
// This replaces multiple repeated instances of a space with a single
var trimmedName = name.replace(/\s+/g,' ');
Index("default", trimmedName);
trim() 函数只会删除字符串的前导和尾随空格,不会删除字符串单词之间的空格
Are you making sure that you're re-assigning your variable after the trim()
?您是否确保在
trim()
之后重新分配变量?
var test = " Hello World ";
test = test.trim(); // "Hello World"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.