最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Filter & Remove Items from JS Array with a Case Insensitive Regex - Stack Overflow

programmeradmin2浏览0评论

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" },

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" },
Share Improve this question asked Jan 17, 2019 at 20:38 Null isTrueNull isTrue 1,9368 gold badges31 silver badges50 bronze badges 4
  • If you know that you want to use regex, have you tried looking up something like "case-insensitive string matching with regex"? I would imagine with that information, you could easily incorporate that into your original example... – Tyler Roper Commented Jan 17, 2019 at 20:41
  • Possible duplicate of ES6: Filter data with case insensetive term – adiga Commented Jan 17, 2019 at 20:46
  • @adiga the referenced question was poorly asked imo. "data like Bla two?". I specifically asked and provided a real code example with a structure of what my problem was. – Null isTrue Commented Jan 17, 2019 at 20:55
  • @NullisTrue the accepted answer is exactly same as the one in this question. – adiga Commented Jan 18, 2019 at 7:22
Add a ment  | 

3 Answers 3

Reset to default 5

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(pareString[, locales[, options]])?

The localeCompare() method returns a number indicating whether a reference string es 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 pare 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);

发布评论

评论列表(0)

  1. 暂无评论