0

I've went through so many posts about this but can't find one that works. I have object keys that I want to search through and see if they have either of two specific words in them and filter them if the exist.

Example:

const obj = {
   time_pop: 'fhfvla',
   icon: 'dsfval',
   home_pops: 'valffg',
   title: 'sdfsdfs',
   pop: 'sfsdfsd',
   rattle: 'sdfdsf',
   pops: 'sfsdfsdf'
}

I want a regex that can find either the word pop || pops in object keys. I'm currently looping through and have the key and am using this as my regex

  const expr = /\b(pop|pops)\b/;

  const only = Object.entries(obj).filter(([k, v]) => {
    return expr.test(k);
  })

The above only works for one word not if it has a _ in it. For example this is not working. time_pop home_pops

They are return false when they should return true because the word pop or pops is in them.

Ehsan
  • 12,655
  • 3
  • 25
  • 44
me-me
  • 5,139
  • 13
  • 50
  • 91

3 Answers3

0

You can use /pops?/ if you want to match partially.

const obj = {time_pop: 'fhfvla',icon: 'dsfval',home_pops: 'valffg',title: 'sdfsdfs',pop: 'sfsdfsd',rattle: 'sdfdsf',pops: 'sfsdfsdf'}

const only = Object.entries(obj).filter(([k, v]) => {
    return /pops?/g.test(k)
})


console.log(only)
Code Maniac
  • 37,143
  • 5
  • 39
  • 60
  • Ah thats smart. Just incase I change the name how would I do it if I had two different keys I wanted to look for. for example. pop or popper – me-me Feb 09 '19 at 05:30
  • `pop(per)?` this will be enough. you can do this when you have one as substring of another one. else just add `|` between them – Code Maniac Feb 09 '19 at 05:31
  • Weird when I put it into my code it didn't work. But when I remove the g from the end it works. Why would that be ? – me-me Feb 09 '19 at 05:50
  • @me-me what didn't work ? g is global flag which means match all the occurrences https://stackoverflow.com/questions/12993629/what-is-the-meaning-of-the-g-flag-in-regular-expressions – Code Maniac Feb 09 '19 at 05:55
  • @me-me if you re use regex with test it will produce problem so you can drop the g flag or you can use like i did – Code Maniac Feb 09 '19 at 05:59
  • Its returning false when it should be return true. When I remove the g it works. const expr = /pops?/; works but when I use const expr = /themes?/g; it does not – me-me Feb 09 '19 at 06:00
  • yes I was using it with test. Do you happen to know why it causes a problem with test ? – me-me Feb 09 '19 at 06:01
  • 1
    @me-me you can read this post https://stackoverflow.com/questions/1520800/why-does-a-regexp-with-global-flag-give-wrong-results# – Code Maniac Feb 09 '19 at 06:05
0

Here, there were also errors (syntax) in your code.

const obj = {
  time_pop: 'fhfvla',
  icon: 'dsfval',
  home_pops: 'valffg',
  title: 'sdfsdfs',
  pop: 'sfsdfsd',
  rattle: 'sdfdsf',
  pops: 'sfsdfsdf'
};

const expr = /pop|pops/;

const only = Object.entries(obj).filter(([k, v]) => expr.test(k));

console.log(only);
Bibberty
  • 4,670
  • 2
  • 8
  • 23
0

In a regular expression, the metacharacter \b represents a word boundary. Essentially, it will only match if there is a non-word character before (pop|pops). An underscore, however, is a word character. Try:

const expr = /(\b|_)(pop|pops)\b/