最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Want to match both uppercase and lowercase letters - Stack Overflow

programmeradmin11浏览0评论

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.

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.

Share Improve this question edited Dec 6, 2012 at 7:50 Engineer 48.8k12 gold badges90 silver badges92 bronze badges asked Dec 6, 2012 at 7:39 EarthEarth 3,5716 gold badges42 silver badges84 bronze badges 1
  • try it var n=str.match(/green/ig); – ke20 Commented Dec 6, 2012 at 7:42
Add a comment  | 

1 Answer 1

Reset to default 19

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

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

demo

发布评论

评论列表(0)

  1. 暂无评论