时间: 2020-08-25|38次围观|0 条评论

下面代码输出什么? 为什么? 改写代码,让其输出hunger, world.

var str = 'hello  "hunger" , hello "world"';var pat =  /".*"/g;  //贪婪模式,先找到“hunger前的引号,然后找到最后,再回溯引号,找到world“后的引号str.match(pat);    //输出[""hunger" , hello "world""]//修改后:var str = 'hello  "hunger" , hello "world"';var pat =  /".*?"/g; //改为非贪婪模式str.match(pat);  

补全如下正则表达式,输出字符串中的注释内容. (可尝试使用贪婪模式和非贪婪模式两种方法)

var  str = '.. <!-- My -- comment \n test --> ..  <!----> .. '//非贪婪模式:var re = /<!--[\w\W]*?-->/g;str.match(re) // '<!-- My -- comment \n test -->', '<!---->'//贪婪模式:var re = /<!--[^>]*-->/g;str.match(re); // '<!-- My -- comment \n test -->', '<!---->'

补全如下正则表达式

//贪婪模式var re = /<[^>]+>/g;//非贪婪模式var re = /<[^>]+?>/g;var str = '<> <a href="/"> <input type="radio" checked> <b>';str.match(re) // '<a href="/">', '<input type="radio" checked>', '<b>'

文章转载于:https://www.jianshu.com/p/88af2dbca3e0

原著是一个有趣的人,若有侵权,请通知删除

本博客所有文章如无特别注明均为原创。
复制或转载请以超链接形式注明转自起风了,原文地址《贪婪模式与非贪婪模式
   

还没有人抢沙发呢~