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. android - Manifest.xml自己生成的GMS服务怎么删掉呢?2. java - git项目迁移到SVN怎么实现的?哪位大神指点指点3. css - 手机app中rem的基准值计算错误4. 站点内容复制额外增加的版权申明,真的很反人类。试问产品自己在用这个站点吗?5. 请问永久和临时重定向有什么区别6. 提示内部服务错误什么问题7. 关于thinkphp 5.1中,ajax提交数据url的格式写法,加花括号就出错,请老师指点8. 老师 我是一个没有学过php语言的准毕业生 我希望您能帮我一下9. 为什么要通过常量的方式拐弯抹角的写呢,直接写DSN之类的不好吗10. 绘制图表的问题

网公网安备