React / Javascript-从输入字段(搜索栏)中删除字符串两端的空格和单词之间的多余空格

问题描述 投票:0回答:1

我有一个搜索栏,可从API提取电影名称的电影。我想修剪名称前后的多余空格,并且单词之间只能有一个空格。

示例:" gran torino "==>"gran torino"" Pirates of the Caribbean " ==> "Pirates of the Caribbean"

search fieldstate value

代码:

const handleSearch = ({ target: { value: inputValue } }) => {
    setSearchInputValue(inputValue);
    myDebouncedFunction(inputValue);
};
<SearchBar 
    type='text' 
    name='query' 
    placeholder='Search movies...' 
    value={searchInputValue} 
    onChange={handleSearch} 
    icon='fa fa-search' />

非工作解决方案-.trim()不允许我在单词之间使用空格。-onBlur也无济于事,因为我不会从搜索栏上迷失方向。 (Remove white spaces from both ends of a string inside a form - React)-我尝试过的几个正则表达式不允许在单词之间使用空格(例如.trim())(https://stackoverflow.com/a/7636022/3186426

我该怎么办?我错过了什么吗?谢谢!

我有一个搜索栏,可从API提取电影名称的电影。我想修剪名称前后的多余空格,并且单词之间只能有一个空格。例如:...

javascript reactjs whitespace trim removing-whitespace
1个回答
0
投票

© www.soinside.com 2019 - 2024. All rights reserved.