如何使用正则表达式从 asddhj123.png.png 拆分为 123.png
我试过下面的正则表达式,但第一个扩展仍然涉及 :(
/[a-zA-z.]|\.(jpg|png)$/
我希望它会回来:
asdasd123.jpg.jpg => 123.jpg
dsds345.png.png => 345.png
最佳答案
此表达式可能会返回所需的输出:
(?i)\d+\.(?:jpe?g|png)
If you wish to explore/simplify/modify the expression, it's been explained on the top right panel of regex101.com. If you'd like, you can also watch in this link, how it would match against some sample inputs.
const regex = /\d+\.(?:jpe?g|png)/gmi;
const str = `asdasd123.jpg.jpg => 123.jpg
asdasd123.jpeg.jpeg => 123.jpeg
dsds345.png.png => 345.png
`;
let m;
while ((m = regex.exec(str)) !== null) {
if (m.index === regex.lastIndex) {
regex.lastIndex++;
}
m.forEach((match, groupIndex) => {
console.log(match);
});
}
关于php - 如何使用正则表达式拆分文件扩展名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57567288/