์ตœ๋Œ€ 1 ๋ถ„ ์†Œ์š”

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);

์ฐธ๊ณ ์ž๋ฃŒ

https://offbyone.tistory.com/174

ํƒœ๊ทธ:

์นดํ…Œ๊ณ ๋ฆฌ:

์—…๋ฐ์ดํŠธ:

๋Œ“๊ธ€๋‚จ๊ธฐ๊ธฐ