title: JavaScript: Can I use the filter function with regular expressions?
body: <p>I tried to find a similar question to avoid creating a duplicate and I couldn’t, but I apologise if I missed any. I've just started learning how to code and I've encountered this problem:</p>
  
  <p>With JavaScript, I want to use the filter arrays method (<a href="https://www.freecodecamp.org/challenges/filter-arrays-with-filter" rel="nofollow noreferrer">https://www.freecodecamp.org/challenges/filter-arrays-with-filter</a>) with a general expression for all non alphanumeric characters.</p>
  
  <p>For example:</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>var newArray = oldArray.filter(function(val) {
  
  return val !== /[\W_]/g;
  
  });</code></pre>
  </div>
  </div>
  </p>
  
  <p>Can I do that? In the mozilla guide (<a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions" rel="nofollow noreferrer">https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions</a>) it mentions you can use regular expressions with replace, and I understand how to do that, but it doesn’t mention filter at all.</p>
  
  <p>To put another less abstract example, this is the code I’m working on:</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>function palindrome(str) {
    
    var splitStr = str.split("");
    
    var filterArray = splitStr.filter(function(val) {
      
      return val !== /[\W_]/g;
    });
    
    return filterArray;
  }
  
  palindrome("ey*e");</code></pre>
  </div>
  </div>
  </p>
  
  <p>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.</p>
  
  <p>If that's the case, why? Why can't one use filter with regular expressions!? Why do we have to use replace instead?</p>