javascript - 正则的截取匹配问题求助
问题描述
srcmainwebappstaticca7ecd95-aa95-4da8-b369-92b66b566958icon.png
想截取从static开始的字符串,请问正则该如何写?感谢
也就是staticca7ecd95-aa95-4da8-b369-92b66b566958icon.png
另外由于url前半段可能会变动,所以最好还是用正则的好
问题解答
回答1:var str=’srcmainwebappstaticca7ecd95-aa95-4da8-b369-92b66b566958icon.png’;alert(str.replace(/^.*?(static.*?)$/ig, ’$1’));回答2:
split(’static’)[1] 这样的吗? 还是必须用正则?
回答3:str.slice(str.search(/static/));
回答4:正则应该用 static.* 就可以,下面是参考代码
const regex = /static.*/g;const str = `srcmainwebappstaticca7ecd95-aa95-4da8-b369-92b66b566958icon.png`;let m;while ((m = regex.exec(str)) !== null) { // This is necessary to avoid infinite loops with zero-width matches if (m.index === regex.lastIndex) {regex.lastIndex++; }// The result can be accessed through the `m`-variable. m.forEach((match, groupIndex) => {console.log(`Found match, group ${groupIndex}: ${match}`); });回答5:
’srcmainwebappstaticca7ecd95-aa95-4da8-b369-92b66b566958icon.png’.split(’static’)[1]
回答6:’srcmainwebappstaticca7ecd95-aa95-4da8-b369-92b66b566958icon.png’.match(/static.*/)// Output: [ 'staticca7ecd95-aa95-4da8-b369-92b66b566958icon.png']
这个问题的亮点:
相关文章:
1. nginx中rewrite和redirect有什么区别呢?2. 网页爬虫 - Python:爬虫的中文编码问题?3. mysql - 索引过滤性不好是由什么原因引起的,应该怎么解决4. javascript - vue生成一维码?求助!!!!!急5. javascript - H5页面怎么查看console信息?6. javascript - 修改表单多选项时和后台同事配合的问题。7. tornado - python使用yield是否能保证协程的顺序性?8. node.js - 用node开启的UDP端口,远程无法访问9. angular.js - items.query is not a function这是怎么回事10. angular.js - angularjs 怎么封装 upload 上传
