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. 搜索出来的值怎样在前台显示不出来,2. javascript - 请问js(非jq)中如果获取元素的高度精准到小数位3. python - 如何使用requests模块在一个post中提交含有多个相同名称的数据?4. js得到了我服务器上word文档的路劲,怎么展示在新的页面上5. 零基础的新手小白想学PHP开发应该从哪儿开始学起来6. thinkphp3.2.3对接新版支付宝7. javascript - 自己开发jQuery插件能不能实现一个插件里有多个功能8. mysql - laravel 子查询的问题9. css - 重写checkbox样式 用到id 与label关联实现 但是现在checkbox循环生成多项 就不能用id了 求解决办法10. mysql5.7 安装后无法设置密码

网公网安备