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. node.js - 使用mongoose的cursor的问题3. javascript - nodemailer连接超时,怎么解决?4. Java。比较字符串时忽略重音5. mysql - Access denied for user ’ODBC’@’localhost’ (using password: NO)6. 请问一下,图片上传成功,但是后台对应文件夹里面却没有图片,这是什么原因?(已部署到服务器)7. 在别的电脑使用JDBC连接其他电脑上安装的mysql数据库8. PHP中的$this代表当前的类还是方法?9. mysql - 订单表,我把它分成好几个,我要查询订单,怎么用一条sql语句高效查询10. php - MySQL数据库设计,获取点赞的人数

网公网安备