INDEX

JavaScript - function - Trim

trim function code in javascript

< html>
< head>
< title>TEST< /title>
< script type="text/javascript">
< !--
// TRIM : space, Double-byte space removal
function trim(argValue){
    return String(argValue).replace(/^[  ]*/gim, "").replace(/[  ]*$/gim, "");
}
//
// LTRIM : space, Double-byte space removal
function ltrim(argValue){
    return String(argValue).replace(/^[  ]*/gim, "");
}
//
// RTRIM : space, Double-byte space removal
function rtrim(argValue){
    return String(argValue).replace(/[  ]*$/gim, "");
}

//
// space removal
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,"");}

function trimTEST(){
	var vTXT = txtA.value;
	var vRESULT;

	vRESULT = trim(vTXT); divRESULT.innerHTML = "result trim(vTXT)_" + vRESULT.length + "< br />"
	vRESULT = ltrim(vTXT); divRESULT.innerHTML += "result ltrim(vTXT):" + vRESULT.length + "< br />"
	vRESULT = rtrim(vTXT); divRESULT.innerHTML += "result rtrim(vTXT):" + vRESULT.length + "< br />"
	vRESULT = vTXT.trim(); divRESULT.innerHTML += "result vTXT.trim():" + vRESULT.length + "< br />"
	vRESULT = vTXT.ltrim(); divRESULT.innerHTML += "result vTXT.ltrim():" + vRESULT.length + "< br />"
	vRESULT = vTXT.rtrim(); divRESULT.innerHTML += "result vTXT.rtrim():" + vRESULT.length + "< br />"

}
//-->
< /script>
< /head>
< body>
< input type="text" name="txtA" size="20" />< br />
< input type="button" value="TRIM test" onclick="trimTEST()" />
< div id="divRESULT">< /div>
< /body>
< /html>


Copyright(c) 2007-2020 coding.dojeun.com All Rights Reserved.