美文网首页
正则表达式

正则表达式

作者: 菲龍探雲 | 来源:发表于2016-08-02 15:41 被阅读31次
    • \d,\w,\s,[a-zA-Z0-9],\b,.,*,+,?,x{3},^$分别是什么?
      \d:digital 匹配数字
      \w:word 匹配字母、数字和下划线_
      \s space 匹配任意的空白符
      [a-zA-Z0-9] 匹配abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ 0123456789
      \b 匹配边界(换行,回车,-)
      . 匹配除换行符以外的任意字符
    • 量词 重复1次或多次
    • 量词 重复0次或多次
      ? 量词 重复0次或1次
      x{3} 匹配3个x
      ^$ 匹配以$开头

    • 贪婪模式和非贪婪模式指什么?
      贪婪模式 默认是贪婪模式 贪婪模式在整个表达式匹配成功的前提下,尽可能多的匹配
      非贪婪模式 需要加上?开启非贪婪模式在整个表达式匹配成功的前提下,尽可能少的匹配
    Paste_Image.png

    代码题

    • 写一个函数trim(str),去除字符串两边的空白字符
    var text="   a b c   ";
    function trim(str) {
        var rexp=/^\s+|\s+$/g;
        var newText;
        newText = str.replace(rexp,"");
      console.log(newText);
    }
    trim(text)
    
    Paste_Image.png

    http://js.jirengu.com/gejagudeni/1/edit


    • 使用实现 addClass(el, cls)、hasClass(el, cls)、removeClass(el,cls),使用正则
    Paste_Image.png
    http://js.jirengu.com/bumogozavo/1/edit
    • 写一个函数isEmail(str),判断用户输入的是不是邮箱
    Paste_Image.png
    http://js.jirengu.com/bumogozavo/2/edit
    • 写一个函数isPhoneNum(str),判断用户输入的是不是手机号
    Paste_Image.png
    http://js.jirengu.com/bumogozavo/3/edit
    • 写一个函数isValidUsername(str),判断用户输入的是不是合法的用户名(长度6-20个字符,只能包括字母、数字、下划线)
    Paste_Image.png
    http://js.jirengu.com/bumogozavo/4/edit
    • 写一个函数isValidPassword(str), 判断用户输入的是不是合法密码(长度6-20个字符,包括大写字母、小写字母、数字、下划线至少两种)
    Paste_Image.png

    http://js.jirengu.com/bumogozavo/5/edit

    • 写一个正则表达式,得到如下字符串里所有的颜色(#121212)
    var re = /*正则...*/
    
    var subj = "color: #121212; background-color: #AA00ef; width: 12px; bad-colors: f#fddee #fd2 "
    
    alert( subj.match(re) )  // #121212,#AA00ef
    
    Paste_Image.png

    http://js.jirengu.com/bumogozavo/6/edit

    • 下面代码输出什么? 为什么? 改写代码,让其输出hunger, world.
    var str = 'hello  "hunger" , hello "world"';
    var pat =  /".*"/g;
    str.match(pat);  
    

    http://js.jirengu.com/bumogozavo/7/edit

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

    http://js.jirengu.com/novixefude/1/edit

    Paste_Image.png
    • 补全如下正则表达式
    var re = /* your regexp */
    
    var str = '<> <a href="/"> <input type="radio" checked> <b>'
    str.match(re) // '<a href="/">', '<input type="radio" checked>', '<b>'
    

    http://js.jirengu.com/novixefude/2/edit

    Paste_Image.png

    本教程版权归菲龍探雲和饥人谷所有,转载须说明来源

    相关文章

      网友评论

          本文标题:正则表达式

          本文链接:https://www.haomeiwen.com/subject/wcftsttx.html