0

I want to filter my array with an exclusion so I did

const exclude = include.filter((d) => {
                let negate1 = '[^'
                let negate2 = ']'
                let negate = negate1.concat(letterexc).concat(negate2)
                let exclRegex = new RegExp(negate,'g')
                console.log(exclRegex) //return --> /[^anyletter]/g
                return d.list.match(exclRegex)
            })

I add [^ and letterexc variable from an input field and ] to make a negation regex, but its not working. not filtering anything.

I read and tried my pattern here https://regexr.com/ and found if my pattern actually works, but not on my own.

Another article from MDN and link says the similar pattern works (with caret after bracket)

screenshot

EDIT: I added a pen link. It's filtering with 2 input field for letter included and excluded. The included filter works fine

louislugas
  • 147
  • 6
  • 1
    What are the values of `include` and `letterexc` and what is the type of `list` ? – The fourth bird Feb 06 '22 at 16:04
  • @Thefourthbird I edited and added a working pen. the `include` is the previous array filter i've done. `letterexc` is a variable from the input field value – louislugas Feb 06 '22 at 16:48

1 Answers1

0

I found it

I change

d.list.match(exclRegex)

to

!d.list.match(exclRegex)

while removing the carat ^ from the concat

complete code excerpt :

const exclude = include.filter((d) => {
                let negate1 = '['
                let negate2 = ']'
                let negate = negate1.concat(letterexc).concat(negate2)
                let exclRegex = new RegExp(negate,'g')
                console.log(exclRegex) //return --> /[^anyletter]/g
                return !d.list.match(exclRegex)
            })
louislugas
  • 147
  • 6