javascript - 正则的截取匹配问题求助
習慣沉默
習慣沉默 2017-07-05 10:53:53
0
6
897
src\main\webapp\static\ca7ecd95-aa95-4da8-b369-92b66b566958\icon.png

想截取从static开始的字符串,请问正则该如何写?感谢

也就是static\ca7ecd95-aa95-4da8-b369-92b66b566958\icon.png

另外由于url前半段可能会变动,所以最好还是用正则的好

習慣沉默
習慣沉默

全部回复(6)
小葫芦

雷雷

刘奇

split('static')[1] 这样的吗? 还是必须用正则?

学霸

str.slice(str.search(/static/));

phpcn_u1582

正则应该用 static.* 就可以,下面是参考代码

const regex = /static.*/g;
const str = `src\main\webapp\static\ca7ecd95-aa95-4da8-b369-92b66b566958\icon.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}`);
    });
ringa_lee

'srcmainwebappstaticca7ecd95-aa95-4da8-b369-92b66b566958icon.png'.split('static')[1]

曾经蜡笔没有小新
'src\main\webapp\static\ca7ecd95-aa95-4da8-b369-92b66b566958\icon.png'.match(/static.*/)

// Output: 
[
  "static\ca7ecd95-aa95-4da8-b369-92b66b566958\icon.png"
]

这个问题的亮点:

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!