I've built a search index in Cloudant. I use trim()
to remove space in string. However, it does not work.
How can I do this?
Update:
I have a JSON object
...
"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.
PS: A small question so it does not need to explain in whole thing.
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.
If you want to remove all white-space in general, you could consider using a Regular Expression replacement via the replace()
function:
// 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()
?
var test = " Hello World ";
test = test.trim(); // "Hello World"
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.