Filter & Remove Items from JS Array with a Case Insensitive Regex

Viewed 846

On the given array:

const arr = [
  { fn: "Steve", ln: "Jobs" },
  { fn: "Dennis", ln: "Rodman" },
  { fn: "Karl", ln: "Malone" },
  { fn: "Vince", ln: "Carter" },
  { fn: "Steve", ln: "King" }
];

If I want to return a new array excluding anyone called Steve, I could run:

const filteredArr = arr.filter(obj => obj.fn != "Steve");

Without using toLowerCase(), How could I write a regex to achieve the same result in the case where Steve names could be lower or upper case?

  { fn: "Steve", ln: "Jobs" },
  { fn: "Dennis", ln: "Rodman" },
  { fn: "Karl", ln: "Malone" },
  { fn: "Vince", ln: "Carter" },
  { fn: "steVe", ln: "King" },
  { fn: "STEVE", ln: "Jordan" },
  { fn: "steve", ln: "Clark" },
3 Answers

Check the strings with RegExp.test() method, and use the i flag (ignore case) in the regular expression.

Note: if you want exact matches, ie ignore strings that include the name - "steven" for example - you can use ^steve$ (see boundaries).

const arr = [{"fst":"Steve","snd":"Jobs"},{"fst":"Dennis","snd":"Rodman"},{"fst":"Karl","snd":"Malone"},{"fst":"Vince","snd":"Carter"},{"fst":"steVe","snd":"King"},{"fst":"STEVE","snd":"Jordan"},{"fst":"steve","snd":"Clark"}];

const pattern = /steve/i; // or /^steven$/i
const result = arr.filter(obj => !pattern.test(obj.fst));

console.log(result);

And you can use the RegExp constructor to make it reusable:

const arr = [{"fst":"Steve","snd":"Jobs"},{"fst":"Dennis","snd":"Rodman"},{"fst":"Karl","snd":"Malone"},{"fst":"Vince","snd":"Carter"},{"fst":"steVe","snd":"King"},{"fst":"STEVE","snd":"Jordan"},{"fst":"steve","snd":"Clark"}];

const removeItem = (arr, str, key = 'fst') => {
  const pattern = new RegExp(str, 'i'); // or new RegExp(`^${str}$`, 'i')

  return arr.filter(obj => !pattern.test(obj[key]));
};

const result = removeItem(arr, 'steve');

console.log(result);

const arr = [
  { fn: "Steve", ln: "Jobs" },
  { fn: "Dennis", ln: "Rodman" },
  { fn: "Karl", ln: "Malone" },
  { fn: "Vince", ln: "Carter" },
  { fn: "Steve", ln: "King" }
];

const filteredArr = arr.filter(obj => !/Steve/i.test(obj.fn));

console.log(filteredArr);

The i regex flag allows you to match results regardless of capitalization (uppercase/lowercase).

Why not use .localeCompare(compareString[, locales[, options]])?

The localeCompare() method returns a number indicating whether a reference string comes before or after or is the same as the given string in sort order.

In the options you may set the sensitivity:

"accent": Only strings that differ in base letters or accents and other diacritic marks compare as unequal. Examples: a ≠ b, a ≠ á, a = A.

const arr = [
  { fn: "Steve", ln: "Jobs" },
  { fn: "Dennis", ln: "Rodman" },
  { fn: "Karl", ln: "Malone" },
  { fn: "Vince", ln: "Carter" },
  { fn: "Steve", ln: "King" }
];

const filteredArr = arr.filter(obj => obj.fn.localeCompare("steve", undefined, 
                                                        { sensitivity: 'accent' }));
console.log(filteredArr);

Related