function doSomething(input) {
return input
.replace(/^\s\s*/, '') // Remove Preceding white space
.replace(/\s\s*$/, '') // Remove Trailing white space
.replace(/([\s]+)/g, '-'); // Replace remaining white space with dashes
}
alert(doSomething(" something with some whitespace "));
/**
* Trim the site input[type=text] fields globally by removing any whitespace from the
* beginning and end of a string on input .blur()
*/
$('input[type=text]').blur(function(){
$(this).val($.trim($(this).val()));
});
function t(k){
if (k[0]==' ') {
return t(k.substr(1,k.length));
} else if (k[k.length-1]==' ') {
return t(k.substr(0,k.length-1));
} else {
return k;
}
}
像这样打电话:
t(" mehmet "); //=>"mehmet"
如果你想过滤特定的字符,你可以定义一个列表字符串:
function t(k){
var l="\r\n\t "; //you can add more chars here.
if (l.indexOf(k[0])>-1) {
return t(k.substr(1,k.length));
} else if (l.indexOf(k[k.length-1])>-1) {
return t(k.substr(0,k.length-1));
} else {
return k;
}
}