我希望有一个更好的方法来写这个?删除[]方括号对我来说是个问题。
alert(CanvasData)//images[]=Base.jpg&images[]=Frame_Clear.png&images[]=Left_Clear.png&images[]=Right_Clear.png&images[]=Lenses_Lenses-Semi-Clear.png&images[]=
var PayName = CanvasData.replace("images[]=", "");
PayName = PayName.replace(/\[.*?\]/g, '');
PayName = PayName.replace(/\&images=/g, ' ');
PayName = PayName.replace(/\.png/g, " &");
PayName = PayName.replace(/\_/g, ' ');
PayName = PayName.substring(8);//remove fist 8 character (Base.jpg)
PayName = PayName.substring(0, PayName.length - 2);//remove last 2 characters // Frame Clear & Left Clear & Right Clear & Lenses Lenses-Semi-Clear &
alert(PayName)// Frame Clear & Left Clear & Right Clear & Lenses Lenses-Semi-Clear
谢谢
最佳答案
用三个全局正则表达式调用replace,其中第一个调用列出应替换为空的每个替代项(即,删除所有的'='和'Base.jpg&'),第二个调用列出应替换为空格的替代项(' images []','_'和'.png'),而第三条则束缚了您的松散结局:
var PayName = CanvasData.replace(/=|Base\.jpg&/g,'')
.replace(/images\[\]|_|\.png/g, ' ')
.replace(/^\s*|\s*&\s*$/g, '');
// => "Frame Clear & Left Clear & Right Clear & Lenses Lenses-Semi-Clear"