search

Home  >  Q&A  >  body text

javascript - A small question about regular expressions, please solve it

  1. Now I have a requirement, for example, let a = 'asdadaasd.jpg'||'Chinese.png';
    2. Now I need to intercept .jpg||.png|| .gif||.jpeg The first 8 digits are '...'.png

let a = 'asdasdqweqweqwadsdasd.png';
let reg = /正则/;
      let strSub =>(filename){
              
            xxx
              return filename
    }
    // 输出的 filename  为 asdasdqweq....png;
世界只因有你世界只因有你2742 days ago685

reply all(1)I'll reply

  • 我想大声告诉你

    我想大声告诉你2017-06-26 10:56:22

    Practice your hands, it is not recommended to use regular rules

    Regular method---code

    let testStr='asdasdqweqweqwadsdasd.png';
    let reg=/(\w{8})\S+([.](?:png|jpeg|gif|jpg))/;
    let newStr=testStr.replace(reg,function(match,p1,p2,offset,string){
          return p1+p2;
        });
    console.log(newStr)

    This is the best way to actually intercept strings code:

    let testStr='asdasdqweqweqwadsdasd.png';
    let index=testStr.lastIndexOf(".");
    let newStr=testStr.substr(0,8)+testStr.substr(index,testStr.length-index);
    console.log(newStr)

    reply
    0
  • Cancelreply