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 app消息推送 百度 极光 个推 信鸽哪个好一些?2. javascript - 为什么当index等于5的时候,不在当前页面跳转到百度?不跳转的代码在倒数第五行3. android - VideoView与百度Map冲突4. javascript - 微信支付问题5. javascript - typescript关于接口,对象字面量额外属性检测问题,为什么使用断言或者变量时就不会检测额外属性?6. html - CSS 怎么筛选没有id的某元素7. javascript - 百度搜索网站,如何让搜索结果显示一张图片加上一段描述,如图;求教8. angular.js - angularjs如何动态改变ng-model值,在controller中获取input中输入的值并组合post发送到后台9. 为什么微信内置浏览器用$_COOKIE取不到值?10. mongoDB怎么把数据导出为csv或excel?
