src\main\webapp\static\ca7ecd95-aa95-4da8-b369-92b66b566958\icon.png
想截取从static开始的字符串,请问正则该如何写?感谢
也就是static\ca7ecd95-aa95-4da8-b369-92b66b566958\icon.png
另外由于url前半段可能会变动,所以最好还是用正则的好
src\main\webapp\static\ca7ecd95-aa95-4da8-b369-92b66b566958\icon.png
想截取从static开始的字符串,请问正则该如何写?感谢
也就是static\ca7ecd95-aa95-4da8-b369-92b66b566958\icon.png
另外由于url前半段可能会变动,所以最好还是用正则的好
正则应该用 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}`);
});
'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"
]
这个问题的亮点:
var url='src\\main\\webapp\\static\\ca7ecd95-aa95-4da8-b369-92b66b566958\\icon.png';
var p=/\bstatic\b.*$/;
var result=url.match(p)[0];
10 回答11.2k 阅读
5 回答4.8k 阅读✓ 已解决
4 回答3.1k 阅读✓ 已解决
2 回答2.7k 阅读✓ 已解决
3 回答2.3k 阅读✓ 已解决
3 回答2.1k 阅读✓ 已解决
2 回答2.6k 阅读✓ 已解决