๐[JavaScript] trim()์ด๋?
trim()์ด๋?
JavaScript
์์ trim()์ด๋ผ๋ ๋ฉ์๋๋ฅผ ์ฌ์ฉํ๊ฒ ๋๋ค.
trim()์ ๋ฌธ์์ด ์ข์ฐ์์ ๊ณต๋ฐฑ์ ์ ๊ฑฐ
ํ๋ ํจ์์ด๋ค.
๋๋ถ๋ถ์ ์ธ์ด์์ ์ ๊ณตํ๊ณ ์์ผ๋ฉฐ, ์ข/์ฐ ์ธก๋ง trimํ๋ ๋ฉ์๋๋ฅผ ์ ๊ณตํ๊ธฐ๋ ํ๋ค๊ณ ํ๋ค.
js(javascript)์์ ์์ฃผ ์ฌ์ฉํ๋๋ฐ, IE8 ์ดํ์์๋ ์ ๊ณต๋์ง ์๋๋ค.
trim() ์์
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>์๋ฐ์คํฌ๋ฆฝํธ TRIM</title>
<script type="text/javascript" src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
<!--[if lte IE 8]>
<script type="text/javascript">
String.prototype.trim = function() {
return this.replace(/^\s+|\s+$/g,"");
}
</script>
<![endif]-->
<script type="text/javascript">
String.prototype.ltrim = function() {
return this.replace(/^\s+/,"");
}
String.prototype.rtrim = function() {
return this.replace(/\s+$/,"");
}
var str = " test ";
console.log(":" + str.trim() + ":");
console.log(":" + $.trim(str) + ":");
console.log(":" + str.ltrim() + ":");
console.log(":" + str.rtrim() + ":");
</script>
</head>
<body>
<h1>TRIM</h1>
</body>
</html>
๊ฒฐ๊ณผ
:test:
:test:
:test :
: test:
trim() ๋ฉ์๋์ ์ฌ์ฉ๋ฒ
์๋ฐ์คํฌ๋ฆฝํธ ๊ธฐ๋ฅ์ ์ฌ์ฉํ ๋์ ์ฝ๋์ด๋ค.
var str = " test ";
var trimStr = str.trim();
JQuery๋ฅผ ์ฌ์ฉํ ๋์ ์ฝ๋์ด๋ค.
var str = " test ";
var trimStr = $.trim(str);
๋๊ธ๋จ๊ธฐ๊ธฐ