Can I use filter with regular expressions?

Hello!

Thanks for reading! I tried to find a similar question to avoid creating a duplicate and I couldn’t, but I apologise if I missed any.

I’m doing the palindrome challenge and came up with this problem:

I want to use the filter method (https://www.freecodecamp.org/challenges/filter-arrays-with-filter) with a general expression for all non alphanumeric characters.

For example:

var newArray = oldArray.filter(function(val) {

return val !== /[\W_]/g;

});

Can I do that? The challenge suggests using replace(), and I understand how to filter the non alphanumeric characters by using replace, but I’m just wondering if I can solve the challenge by using filter instead.

This is what I’ve done so far:

function palindrome(str) {

  var lowCase = str.toLowerCase();
  
  var splitStr = lowCase.split("");
  
  var filterArray = splitStr.filter(function(val) {
    
    return val !== /[\W_]/g;
  });
  
  return filterArray;
}

palindrome("ey*e");

If I’m doing things right so far, the function should return [“e”, “y”, “e”]. But it returns [“e”, “y”, “*”, “e”] (as if I hadn’t filtered it at all). I just wonder if I’ve made a mistake in my code, or if one simply can’t use filter with regular expressions.

In the mozilla guide to regular expressions it mentions you can use them with replace, but it doesn’t mention filter at all. Why? Why can’t you use filter with regular expressions?!

I’m sorry for the length of this question! And thanks to anyone who can shed a light into this conundrum :smile:

You can use regex in a filter function, but you don’t use equality operators (like !==).

Hi ArielLeslie!

Thanks so much for posting! I have a few follow ups if you don’t mind me asking:

  • The example provided by FCC for the filter method includes an equality operator:

The following code is an example of using filter to remove array elements that are equal to five:

array = array.filter(function(val) {
  return val !== 5;
});

This code seems to work fine so I’m not sure I understood what you meant by not using equality operators.

  • Thanks for sharing the link. I had no idea that test() existed and it seems really useful for checking if a string contains non alphanumeric characters (for example) but I don’t know how you would use it to filter those characters out.

I’m sorry I’m still learning really slowly so I have trouble understanding your answer :confused: I hope you don’t mind all my follow up questions!

The filter function works by returning a truthy or falsy value. The reason that your use of the equality operator doesn’t work because equality operations don’t work for comparing a string to a regular expression.

1 Like

If you want to use filter, consider using the test method of regexes, for example:

 /^[A-Z]$/.test('A'); //true
 /^[A-Z]$/.test('!'); //false
1 Like

Hi again! Honestly, thanks so much for taking the time to help! Now I understand what you meant completely.
I realise I hadn’t fully grasped the filter function until now, and by combining it with test() like you suggested, I could solve my problem. Have a great day!!! :smile: :heart:

Hi lionel-rowe! Thanks so much for your answer, it provided insight and helped me understand how to use filter with regular expressions! I’m selecting ArielLeslie’s first post as the correct answer since it includes where I went wrong, as well as the test method suggestion. However I wanted to thank you as well because I needed two people explaining it to finally understand it :blush:

1 Like

Glad I could help. Happy coding!

1 Like