Earth
Earth

Reputation: 3571

Want to match both uppercase and lowercase letters

My Code:

I tried the following code

var str="I like green and want to build a GREENERY Earth with greening!"; 
var n=str.match(/green/g);

It is giving the result as

green,green

But I need the result as

green,GREEN,green

That is, I want to match both uppercase and lowercase letters. In this case, totally 3 green words are found.

Upvotes: 2

Views: 8145

Answers (1)

Engineer
Engineer

Reputation: 48793

Use i flag, which will ignore the case-sensitiveness:

var n=str.match(/green/gi); 
//                      ^----here it is

demo

Upvotes: 19

Related Questions