function replaceLeadTrailChar(str, ch) {
var str1 = str;
var regx1 = new RegExp("^ +"); // 此处的“ ”(空格)不能用“\s”来代替,否则会把全角空格也替换掉
var str2 = "";
var str3 = str1.replace(regx1,"");
if (regx1.test(str1)) {
str2 = regx1.exec(str1)[0].replace(/\s/g, ch);
}
var str4 = str2 + str3;
var regx2 = new RegExp(" +$"); // 此处的“ ”(空格)不能用“\s”来代替,否则会把全角空格也替换掉
var str5 = str4.replace(regx2,"");
var str6 = ""
if (regx2.test(str4)) {
str6 = regx2.exec(str4)[0].replace(/\s/g, ch);
}
return str5 + str6;
}
正则表达式说明:http://home.a00.itscom.net/hatada/js-tips/regexp.html?vm=r&s=1