简体   繁体   English

不要在我的数组中使用空格[Javascript]

[英]Don't take whitespace in my array [Javascript]

it's my first post then sorry for that. 这是我的第一篇文章,对此深表歉意。 I try to explain you the concept and the objectif 我尝试向您解释概念和对象

I have this code for a FAQ. 我有此代码的常见问题解答。 The objectif is to search your problem with few words in a search bar (with an input). 目的是在搜索栏中(带有输入项)用少量单词搜索您的问题。 You write 1 or 2 words with a "whitespace" and all topic who match with the words, they display them and others they display:none. 您用“空白”写1个或2个单词,并且所有与此单词匹配的主题都将显示它们,而其他主题则显示:无。

My question is : Before to display them, I try to push the results good in a array[]. 我的问题是:在显示它们之前,我尝试将结果良好地放入array []中。 But the problem is with my input, he takes the whitespace and he returns all results. 但是问题出在我的输入上,他使用空格,然后返回所有结果。 Then I trying to split(), to regexp my input, in Javascript , for delete this whitespace and to returns only the words who the client write but Im a newbie. 然后,我尝试在Javascript中使用split()来对表达式的输入进行正则表达式,以删除该空白并仅返回客户端写的单词,而我只是一个新手。

Im here if you got more questions. 我在这里,如果您还有其他问题。 Thx 谢谢

filterSearchFaq = inputSearchFaq.value.toUpperCase();
splitInput = filterSearchFaq.split(/\s+/);
ulSearchFaq = document.getElementById("listQuestionResponseFaq");
liSearchFaq = ulSearchFaq.getElementsByTagName("li");
var countFound = 0;
var array = [];

    for (var i = 0; i < splitInput.length; i++) {                   
        var word = splitInput[i];                   
            for (var j = 0; j < liSearchFaq.length; j++) {
               var panelClass = 
               liSearchFaq[j].getElementsByClassName("panel");
                  if (null != panelClass[0]) {
                      panel = liSearchFaq[j].getElementsByClassName("panel")[0];
                      searchIndex =  panel.innerHTML.toUpperCase().indexOf(word);
                        if (searchIndex > -1) {
                                array.push(searchIndex);
                            }                                                           
                        }        
                    }

            }

console.log 的console.log

You can see the second word, its a whitespace because I press my space bar, but I want to delete this whitespace when the client will press the spacebar 您可以看到第二个单词,它是一个空格,因为我按了空格键,但是当客户端将按下空格键时,我想删除此空格

[from comments] if I don't put any words in my input or I put a space after my first or second world, I got a : " " in my array and it returns all my answers. [摘自评论]如果我在输入的内容中没有输入任何字词,或者在第一个或第二个世界后添加空格,则数组中会出现一个:“”,它将返回所有答案。 My questionn is How can i delete this white space in my array. 我的问题是如何删除阵列中的空白。

One easy way around that would be to switch from splitting to matching: 解决这个问题的一种简单方法是从拆分切换为匹配:

" foo bar baz ".match(/(\S+)/g)

this will capture all the groups of non-whitespace characters only. 这将仅捕获所有非空白字符组。

This is basically a reversal of the logic - instead of splitting at single white space characters, which can create “empty” elements in the result, you just match consecutive groups of non-WS characters - that way it doesn't matter any more if there's one or one hundred white space characters between them. 这基本上是一种逻辑上的逆转-而不是分割成单个空格字符(可以在结果中创建“空”元素),您只需匹配连续的非WS字符组-这样就不再重要了它们之间有一百个空格字符。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM