JavaScriptで文字列の左右側の空白文字を削除する

1.javascriptの書き方
<script language="javascript">
String.prototype.trim=function(){
return this.replace(/(^\s*)|(\s*$)/g, “");
}
String.prototype.ltrim=function(){
return this.replace(/(^\s*)/g,"");
}
String.prototype.rtrim=function(){
return this.replace(/(\s*$)/g,"");
}
</script>

2.メソッドの書き方
<script type="text/javascript">
function trim(str){
return str.replace(/(^\s*)|(\s*$)/g, “");
}
function ltrim(str){
return str.replace(/(^\s*)/g,"");
}
function rtrim(str){
return str.replace(/(\s*$)/g,"");
}
</script>

JavaScript

Posted by arkgame