正则表达式

338 阅读3分钟

#问答

  • ####\d,\w,\s,[a-zA-Z0-9],\b,.,*,+,?,x{3},^$分别是什么? \d:匹配数字 \w:匹配字母或数字或下划线或汉字 \s:匹配任意的空白符 [a-zA-z0-9]:匹配任意字母和数字 \b:匹配单词的开始或结束 . :匹配除换行符以外的任意字符 *:重复零次或更多次 +:重复一次或更多次 ? :重复零次或一次 x{3}:重复三次x ^$:匹配行的开始处和结束处
  • ####贪婪模式和非贪婪模式指什么? 贪婪模式:当正则表达式中包含能接受重复的限定符时,通常的行为是(在使整个表达式能得到匹配的前提下)匹配尽可能多的字符。以这个表达式为例:a.*b,它将会匹配最长的以a开始,以b结束的字符串。如果用它来搜索aabab的话,它会匹配整个字符串aabab。这被称为贪婪匹配 懒惰模式:匹配任意数量的重复,但是在能使整个匹配成功的前提下使用最少的重复

#代码

  • 写一个函数trim(str),去除字符串两边的空白字符 代码: function trim(str){ newstr = str.replace(/^\s*|\s*$/,''); return newstr } var a = ' asdas dasdasd ' console.log(trim(a))

  • 使用实现 addClass(el, cls)hasClass(el, cls)removeClass(el,cls),使用正则 //提示: el为dom元素,cls为操作的class, el.className获取el元素的class 代码: //提示: el为dom元素,cls为操作的class, el.className获取el元素的class

      function addClass(el, cls) {
          if (!hasClass(el, cls)) {
              el.className += " " + cls;
          }
      }
    
      function hasClass(el, cls) {
          var reg = new RegExp('\\b' + cls + '\\b', 'g');
          return reg.test(el.className);
      }
    
      function removeClass(el, cls) {
          var reg = new RegExp('\\b' + cls + '\\b', 'g'),
              tmp = node.className.replace(reg, '').replace(/\s{2,}/g, ' '); //把两个以上的空格替换为一个空格
          el.className = trim(tmp);
      }
    
  • 写一个函数isEmail(str),判断用户输入的是不是邮箱 代码: function isEmail(str){ reg = /^[a-z0-9]+([._\-][a-z0-9])@([a-z0-9]+[-a-z0-9]*[a-z0-9]+.){1,63}[a-z0-9]+$/g return reg.test(str) } console.log(isEmail('a'))//false console.log(isEmail('123@qq.com'))//true console.log(isEmail('zzzzzzzdadasdqweqwedq@163.com'))//true console.log(isEmail('w-eqwdasd@qq.com'))//true

  • 写一个函数isPhoneNum(str),判断用户输入的是不是手机号 代码: function isPhone(str){ reg = /^1[3|5|7|8]\d{9}$/g return reg.test(str) } console.log(isPhone('110'))//false console.log(isPhone('13893000000'))//true console.log(isPhone('1881333900'))//false console.log(isPhone('12345678901'))//false console.log(isPhone('17045678901'))//true

  • 写一个函数isValidUsername(str),判断用户输入的是不是合法的用户名(长度6-20个字符,只能包括字母、数字、下划线) 代码: function isValidUsername(str){ reg = /^[0-9a-zA-Z|]{6,20}$/g return reg.test(str) } console.log(isValidUsername('111111!'))//false console.log(isValidUsername('zxw1111'))//true console.log(isValidUsername('zxw_2131242153243241'))//true console.log(isValidUsername('zxw?aweeqweqw'))//false console.log(isValidUsername('zxczxwZZX'))//true

  • 写一个函数isValidPassword(str), 判断用户输入的是不是合法密码(长度6-20个字符,包括大写字母、小写字母、数字、下划线至少两种) 代码: function isValidPassword(str){ if(str.length<6||str.length>20){ return false } if(/[^a-zA-Z0-9_]/.test(str)){ return false } if(/(^[a-z]+$|^[A-Z]+$|^\d+$|^_+$)/.test(str)){ return false } return true } console.log(isValidPassword('zxw1992513'))

  • 写一个正则表达式,得到如下字符串里所有的颜色(#121212) var re = /正则.../

      var subj = "color: #121212; background-color: #AA00ef; width: 12px; bad-colors: f#fddee #fd2 "
    
      alert( subj.match(re) )  // #121212,#AA00ef
    

代码: var re = /#[0-9a-fA-F]{6}/g/正则.../

    var subj = "color: #121212; background-color: #AA00ef; width: 12px; bad-colors: f#fddee #fd2 "

    alert( subj.match(re) )  // #121212,#AA00ef
  • 下面代码输出什么? 为什么? 改写代码,让其输出hunger, world. var str = 'hello "hunger" , hello "world"'; var pat = /"."/g; str.match(pat);
    因为是贪婪模式,会把最外""中内容输出,即输出"hunger" , hello "world" 代码: //方法一 var str = 'hello "hunger" , hello "world"'; var pat = /"\w
    "/g; str.match(pat);
    console.log(str.match(pat)) //方法二 var str = 'hello "hunger" , hello "world"'; var pat = /".*?"/g; str.match(pat);
    console.log(str.match(pat))

  • 补全如下正则表达式,输出字符串中的注释内容. (可尝试使用贪婪模式和非贪婪模式两种方法) str = '.. <!-- My -- comment \n test --> .. .. ' re = /.. your regexp ../

      str.match(re) // '<!-- My -- comment \n test -->', '<!---->'
    

代码: //贪婪模式 str = '.. <!-- My -- comment \n test --> .. .. ' re = //g str.match(re) // '<!-- My -- comment \n test -->', '' console.log(str.match(re)) //非贪婪模式 str = '.. <!-- My -- comment \n test --> .. .. ' re = //g str.match(re) // '<!-- My -- comment \n test -->', '' console.log(str.match(re))

  • 补全如下正则表达式 var re = /* your regexp */

      var str = '<> <a href="/"> <input type="radio" checked> <b>'
      str.match(re) // '<a href="/">', '<input type="radio" checked>', '<b>'
    

代码: var re = /<[^>]+>/g

    var str = '<> <a href="/"> <input type="radio" checked> <b>'
    str.match(re) // '<a href="/">', '<input type="radio" checked>', '<b>'
    console.log(str.match(re))