user13618351
user13618351

Reputation:

How to filter all lowercase letters from a string having special characters?

The input string is - "my&friend&Paul has heavy hats! &".

And I want to get rid of everything except lowercase letters like: "myfriendaulhasheavyhats"

I split it and applied the toLowerCase() method, but it doesn't work with special characters. Need some help!!!

Upvotes: 1

Views: 567

Answers (3)

Majed Badawi
Majed Badawi

Reputation: 28414

You can use .replace function as follows:

let str = "my&friend&Paul has heavy hats! &";
str = str.replace(/[^a-z]+/g, '');
console.log(str);

Upvotes: 1

Not the best answer
Not the best answer

Reputation: 301

Simply replace all none a-z characters by an empty string

"my&friend&Paul has heavy hats! &".replace(/[^a-z]/g, "");

Upvotes: 3

Shubham Dixit
Shubham Dixit

Reputation: 1

Simple regex with join

let str = "my&friend&Paul has heavy hats! &";

console.log(str.match(/[a-z]/g).join(""))

Upvotes: 0

Related Questions