How to filter JS only with numbers and letters (no special characters)

Asked

Viewed 137 times

0

I need help to do a function where I put a code to search and it shows me the result. The problem is that it currently only brings the result if I put it exactly as it is in JSON.

Ex.: if I search "9 190 087 006" it will bring me the line that is this code, but if I put the same code "9190087006" (no spaces), it does not find.

Can help me improve the code so that regardless of any special character it can bring the results, considering only the numbers and letters to do the search?

<textarea id='txt' style="height: 200">
9 190 087 006
9190087006
13067/5
130675
VR-B190
VR B190
</textarea>
<br>
<button onclick='limpar()'>Clear</button>
<button onclick='render()'>Pesquisa</button>
<!-- <input type="" name="" id='pesq'> -->

<div id="lista"></div>

<script>
    
    x = [

{"Codigo":"T001","Marca":"AAAAAA","Cod":{" Marca":"9 190 087 006"}},
{"Codigo":"T001","Marca":"AAAAAA","Cod":{" Marca":"9-190-085-001"}},
{"Codigo":"T001","Marca":"AAAAAA","Cod":{" Marca":"9.190.087.011"}},
{"Codigo":"T001","Marca":"AAAAAA","Cod":{" Marca":"13067/5"}},
{"Codigo":"T001","Marca":"AAAAAA","Cod":{" Marca":"2500216"}},
{"Codigo":"T002","Marca":"AAAAAA","Cod":{" Marca":"130216"}},
{"Codigo":"T002","Marca":"AAAAAA","Cod":{" Marca":"IK420HD"}},
{"Codigo":"T002","Marca":"AAAAAA","Cod":{" Marca":"VR-B190"}},
{"Codigo":"T002","Marca":"AAAAAA","Cod":{" Marca":"VRB190"}},
{"Codigo":"T003","Marca":"AAAAAA","Cod":{" Marca":"IB305"}},
{"Codigo":"T003","Marca":"AAAAAA","Cod":{" Marca":"9 190 083 002"}},
{"Codigo":"T003","Marca":"AAAAAA","Cod":{" Marca":"1197 BE3 002"}}

]

    function pesquisa(valor){
        // valor = this.value
        
        resultados = []
        for(i of x){

            reg = new RegExp(valor,"g")

            if(valor == i.Cod[' Marca'].match(reg)){
                resultados.push(i)
            }
        }
        return resultados
        // console.log(resultados)
        // lista.innerHTML = resultados
    }

    function add(str){
        j = []
        json1 = pesquisa(str)
        div = document.createElement("div")
        div.innerHTML = JSON.stringify(json1)
        lista.appendChild(div)
    }

    function render(){
        t = txt.value.split("\n")
        for(o of t){
            add(o)
        }
    }
    render()

    function limpar(){
        lista.innerHTML = ""
    }

</script>

2 answers

1


I put an excerpt in if, basically what you did, eliminating the special characters in one of the conditions:

if(valor == i.Cod[' Marca'].replace(/[^a-z0-9]/gi,'').match(reg) || valor == i.Cod[' Marca'].match(reg))

I kept what you did in the or because only in regex it does not return with the special characters, so it returns both.

Fiddle of the result: https://jsfiddle.net/w3jq8g2t/

1

A possible solution to this problem is to create new strings without these special characters. For this we can use a regex like /[^A-Za-z0-9]/g, that gives match on any character that nay letter or number. Then we use the replace() to swap for an empty character:

<textarea id='txt' style="height: 200">
9 190 087 006
9190087006
13067/5
130675
VR-B190
VR B190
</textarea>
<br>
<button onclick='limpar()'>Clear</button>
<button onclick='render()'>Pesquisa</button>
<!-- <input type="" name="" id='pesq'> -->

<div id="lista"></div>

<script>
    
    x = [

{"Codigo":"T001","Marca":"AAAAAA","Cod":{" Marca":"9 190 087 006"}},
{"Codigo":"T001","Marca":"AAAAAA","Cod":{" Marca":"9-190-085-001"}},
{"Codigo":"T001","Marca":"AAAAAA","Cod":{" Marca":"9.190.087.011"}},
{"Codigo":"T001","Marca":"AAAAAA","Cod":{" Marca":"13067/5"}},
{"Codigo":"T001","Marca":"AAAAAA","Cod":{" Marca":"2500216"}},
{"Codigo":"T002","Marca":"AAAAAA","Cod":{" Marca":"130216"}},
{"Codigo":"T002","Marca":"AAAAAA","Cod":{" Marca":"IK420HD"}},
{"Codigo":"T002","Marca":"AAAAAA","Cod":{" Marca":"VR-B190"}},
{"Codigo":"T002","Marca":"AAAAAA","Cod":{" Marca":"VRB190"}},
{"Codigo":"T003","Marca":"AAAAAA","Cod":{" Marca":"IB305"}},
{"Codigo":"T003","Marca":"AAAAAA","Cod":{" Marca":"9 190 083 002"}},
{"Codigo":"T003","Marca":"AAAAAA","Cod":{" Marca":"1197 BE3 002"}}

]

    function pesquisa(valor){
        // valor = this.value
        
        resultados = []
        for(i of x){

            const formattedInput = valor.replace(/[^A-Za-z0-9]/g, '');
            const formattedCodMarca = i.Cod[' Marca'].replace(/[^A-Za-z0-9]/g, '');

            if(formattedInput === formattedCodMarca){
                resultados.push(i)
            }
        }
        return resultados
        // console.log(resultados)
        // lista.innerHTML = resultados
    }

    function add(str){
        j = []
        json1 = pesquisa(str)
        div = document.createElement("div")
        div.innerHTML = JSON.stringify(json1)
        lista.appendChild(div)
    }

    function render(){
        t = txt.value.split("\n")
        for(o of t){
            add(o)
        }
    }
    render()

    function limpar(){
        lista.innerHTML = ""
    }

</script>

  • thanks very much it worked out friend

Browser other questions tagged

You are not signed in. Login or sign up in order to post.