我正在尝试构建一个正则表达式来捕获 S3 文件名上传的有用部分。我使用了正则表达式生成器,到目前为止我进行了此测试(这会导致 javascript 抛出错误):
/[A-Za-z]++[^\.\w][^\.]++|(?<=_)\w++(?=\.)/g
以下是我正在使用的一些示例字符串(带有需要匹配的模式):
"MTxoZbRRUu9BfQLvAWwP_Bruntwood Leeds Digital Festival ad.pdf" // desired match "Bruntwood Leeds Digital Festival ad"
"bbZRU3329BfXXvvAWwP_short-video.mp4" // desired match "short-video"
"zQZFnWVcRUbFNGyGdIP0_MGI-Artificial-Intelligence-Discussion-slides.pptx" // desired match "MGI-Artificial-Intelligence-Discussion-slides"
如果有帮助 - 我需要在 javascript 上运行此正则表达式测试。
const filename = "bbZRU3329BfXXvvAWwP_short-video.mp4";
const match = filename.match(regex);
console.log(match); // "short-video"
谢谢!
最佳答案
根据您的示例,您可以使用更简单的正则表达式:
const regex = /_([^.]+)/;
const inputs = [
"MTxoZbRRUu9BfQLvAWwP_Bruntwood Leeds Digital Festival ad.pdf", // desired match "Bruntwood Leeds Digital Festival ad"
"bbZRU3329BfXXvvAWwP_short-video.mp4", // desired match "short-video"
"zQZFnWVcRUbFNGyGdIP0_MGI-Artificial-Intelligence-Discussion-slides.pptx" // desired match "MGI-Artificial-Intelligence-Discussion-slides"
];
for (const input of inputs) {
const match = input.match(regex);
console.log(match[1]);
}
关于javascript - 正则表达式 - 无效表达式错误 "Nothing to repeat at...",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49718426/