我有这样的网址:
<p class="price-small">Don't miss this special offer<br />
Offer ends Tues, Jul 22 at 11:59pm PST</p>
我需要这样提取原始文件名:
http://localhost/static/1941ce/917985481634941-350x_.png
http://localhost/static/1941ce/917985481634941-350x120.png
用Java脚本执行此操作的好方法是什么? 干杯!
答案 0 :(得分:0)
您应该为此使用正则表达式:
var str1 = 'http://localhost/static/1941ce/917985481634941-350x_.png';
var str2 = 'http://localhost/static/1941ce/917985481634941-350x120.png';
var postfixRe = /-[0-9x_]*\./i;
console.log(str1.replace(postfixRe, '.'));
console.log(str2.replace(postfixRe, '.'));
如果您不确定是否只会出现'-':
var str1 = 'http://localhost/static/1941ce/917985481634941-350x_.png';
var str2 = 'http://localhost/static/1941ce/917985481634941-350x120.png';
var postfixRe = /\/(\d+)-[0-9x_]*\./i;
console.log(str1.replace(postfixRe, '/$1.'));
console.log(str2.replace(postfixRe, '/$1.'));