Uzma Khan Uzma Khan - 2 months ago 33x
Javascript Question

Why we are using _ in this expression str.replace(/[\W_]/g, '').toLowerCase(); We could have used /[\W]/g also but why are we using underscore?

It is a javascript question.I was solving palindromes question on freecodecamp.Let me write the full code here:

function palindrome(str) {
var normalizedStr = str.replace(/[\W_]/g, '').toLowerCase();
var reverseStr = normalizedStr.split('').reverse().join('');
return normalizedStr === reverseStr;


\W match any non-word character [^a-zA-Z0-9_]

_ the literal character _

so this regex will keep in your string only letters and numbers