David
David

Reputation: 323

Efficient way to remove delimiters from string in JavaScript

I have to write several lines of code to remove delimiters from a string. Is there a more efficient way to remove all delimiters? Thanks.

    str = str.toLowerCase();
    str = str.replace(/ /g,'');
    str = str.replace(/\*/g, '');
    str = str.replace(/_/g, '');
    str = str.replace(/#/g, '');
    //etc....

Upvotes: 0

Views: 3747

Answers (1)

RomanPerekhrest
RomanPerekhrest

Reputation: 92854

Use character class:

str = str.toLowerCase().replace(/[ *_#]/g, '');

http://www.regular-expressions.info/charclass.html

Upvotes: 8

Related Questions