Joe
Joe

Reputation: 1159

Combining non-word and underscore patterns to match any non-alphanumeric characters

In javascript, I want to weed out all non-alphanumeric characters. here's my code:

function (inpt) {
        return inpt.replace(/[^\w]/gi, "").replace(/_/g, "");
    }

This code works 100% correct. Removes all non-alphanumeric characters.

The \w still allows underscore, which is why I chained two replace statements together.

I then tried combining the Regular Expressions like this, and now underscores are no longer filtered.

function (inpt) {
        return inpt.replace(/[^\w_]/gi, "");     // note:  added _
    }

What am I doing wrong?

Upvotes: 1

Views: 101

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 627087

Your [^\w_] pattern represents a negated character class that matches any char but a word (letter, digit or _) and _ chars.

The [^\w] pattern is equal to \W. So, you may use

/[\W_]/g

See the regex demo.

Here, [\W_] is a positive character class that matches either a non-word char or a _.

JS demo:

var str = "some%_1string*";
console.log(str.replace(/[\W_]+/g, ''));

Note that .replace(/[\W_]+/g, '') removes the chars you need a bit quicker than .replace(/[\W_]/g, '') since + quantifier matches 1+ consecutive occurrences of matching characters and removes them all in one go.

Upvotes: 2

Related Questions