[英]JS trim() function in cloudant
我在 Cloudant 中構建了一個搜索索引。 我使用trim()
刪除字符串中的空格。 但是,它不起作用。
我怎樣才能做到這一點?
更新:
我有一個 JSON 對象
...
"attributeArray": [
{
"name": "this is a web authentication"
}
}
...
我已經成功提取了“名稱”。 我想刪除“名稱”中的空格,然后為文檔創建一個搜索索引。 假設已經提取了“名稱”。
var index=name.trim();
Index("default", index);
當我查詢時,系統顯示:
{
"id": "06xxxx",
"fields": [
" this is a web authentication"
]
}
我得出結論,函數trim()
不起作用。
PS:一個小問題,所以不需要全部解釋。
根據定義, trim()
函數只會刪除字符串中的前導和尾隨空格,這在這種情況下可能不是您所需要的。
如果您想刪除所有空白,您可以考慮通過replace()
函數使用正則表達式替換:
// This will remove all white-space from your input string
var output = input.replace(/\s/g,'');
更新后
您的代碼看起來更像是您想用一個空格替換一個空格的多個實例,這仍然可以通過與原始表達式略有不同的表達式來完成:
// This replaces multiple repeated instances of a space with a single
var trimmedName = name.replace(/\s+/g,' ');
Index("default", trimmedName);
trim() 函數只會刪除字符串的前導和尾隨空格,不會刪除字符串單詞之間的空格
您是否確保在trim()
之后重新分配變量?
var test = " Hello World ";
test = test.trim(); // "Hello World"
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.