32์žฅ String

ํ‘œ์ค€ ๋นŒํŠธ์ธ ๊ฐ์ฒด์ธ String์€ ์›์‹œ ํƒ€์ž…์ธ ๋ฌธ์ž์—ด์„ ๋‹ค๋ฃฐ ๋•Œ ์œ ์šฉํ•œ ํ”„๋กœํผํ‹ฐ์™€ ๋ฉ”์„œ๋“œ๋ฅผ ์ œ๊ณตํ•œ๋‹ค.

32.1 String ์ƒ์„ฑ์ž ํ•จ์ˆ˜

ํ‘œ์ค€ ๋นŒํŠธ์ธ ๊ฐ์ฒด์ธ String ๊ฐ์ฒด๋Š” ์ƒ์„ฑ์ž ํ•จ์ˆ˜ ๊ฐ์ฒด๋‹ค. ๋”ฐ๋ผ์„œ new ์—ฐ์‚ฐ์ž์™€ ํ•จ๊ป˜ ํ˜ธ์ถœํ•˜์—ฌ String ์ธ์Šคํ„ด์Šค๋ฅผ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค.

์ธ์ˆ˜๋ฅผ ์ „๋‹ฌํ•˜์ง€ ์•Š๊ณ  new ์—ฐ์‚ฐ์ž์™€ ํ•จ๊ป˜ ํ˜ธ์ถœํ•˜๋ฉด ๋นˆ ๋ฌธ์ž์—ด์„ ํ• ๋‹นํ•œ String ๋ž˜ํผ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•œ๋‹ค. ์ธ์ˆ˜๋ฅผ ์ „๋‹ฌํ•˜๊ณ  new ์—ฐ์‚ฐ์ž์™€ ํ•จ๊ป˜ ํ˜ธ์ถœํ•˜๋ฉด ์ธ์ˆ˜๋กœ ์ „๋‹ฌ๋ฐ›์€ ๋ฌธ์ž์—ด์„ ํ• ๋‹นํ•œ String ๋ž˜ํผ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•œ๋‹ค.

const strObj = new String();
console.log(strObj); // String {length: 0, [[PrimitiveValue]]: ""}

const strObj2 = new String('Han');
console.log(strObj2); // {0: "H", 1: "a", 2: "n", length: 3, [[PrimitiveValue]]: "Han"}

String ๋ž˜ํผ ๊ฐ์ฒด๋Š” ๋ฐฐ์—ด๊ณผ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ length ํ”„๋กœํผํ‹ฐ์™€ ์ธ๋ฑ์Šค๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ์ˆซ์ž ํ˜•์‹์˜ ๋ฌธ์ž์—ด์„ ํ”„๋กœํผํ‹ฐ ํ‚ค๋กœ, ๊ฐ ๋ฌธ์ž๋ฅผ ํ”„๋กœํผํ‹ฐ ๊ฐ’์œผ๋กœ ๊ฐ–๋Š” ์œ ์‚ฌ ๋ฐฐ์—ด ๊ฐ์ฒด์ด๋ฉด์„œ ์ดํ„ฐ๋Ÿฌ๋ธ”์ด๋‹ค. ๋”ฐ๋ผ์„œ ๋ฐฐ์—ด๊ณผ ์œ ์‚ฌํ•˜๊ฒŒ ์ธ๋ฑ์Šค๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๊ฐ ๋ฌธ์ž์— ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋‹ค.

๋‹จ, ๋ฌธ์ž์—ด์€ ์›์‹œ ๊ฐ’์œผ๋กœ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์—†๋‹ค.

String ์ƒ์„ฑ์ž ํ•จ์ˆ˜์˜ ์ธ์ˆ˜๋กœ ๋ฌธ์ž์—ด์ด ์•„๋‹Œ ๊ฐ’์„ ์ „๋‹ฌํ•˜๋ฉด ์ธ์ˆ˜๋ฅผ ๋ฌธ์ž์—ด๋กœ ๊ฐ•์ œ ๋ณ€ํ™˜ํ•œ ํ›„, [[StringData]] ๋‚ด๋ถ€๋ณ€์Šฌ๋กฏ์—ํ™˜๋œ ๋ฌธ์ž์—ด์„ ํ• ๋‹นํ•œ String ๋ž˜ํผ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•œ๋‹ค.

let strObj = new String(123);
console.log(strObj)
//String {0: "1", 1: "2", 2: "3", length: 3, [[PrimitiveValue]]: "123" }

strObj = new String(null);
console.log(strObj);
//String {0: "n", 1: "u", 2: "l", 3: "l", length: 4, [[PrimitiveValue]]: "null"}

new ์—ฐ์‚ฐ์ž๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ณ  String ์ƒ์„ฑ์ž ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜๋ฉด ๋ฌธ์ž์—ด์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค. ์ด๋ฅผ ์ด์šฉํ•˜์—ฌ ๋ช…์‹œ์ ์œผ๋กœ ํƒ€์ž…์„ ๋ณ€ํ™˜ํ•˜๊ธฐ๋„ ํ•œ๋‹ค.

String(1) // "1"
String(NaN) // "NaN"
String(Infinity) // "Infinity"

String(true) // "true"
String(false) // "false"

32.1 length ํ”„๋กœํผํ‹ฐ

length ํ”„๋กœํผํ‹ฐ๋Š” ๋ฌธ์ž์—ด์˜ ๋ฌธ์ž ๊ฐœ์ˆ˜๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

'Hello'.length // 5
'hi'.length // 2

String ๋ž˜ํผ ๊ฐ์ฒด๋Š” ๋ฐฐ์—ด๊ณผ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ length ํ”„๋กœํผํ‹ฐ๋ฅผ ๊ฐ–๋Š”๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ธ๋ฑ์Šค๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ์ˆซ์ž๋ฅผ ํ”„๋กœํผํ‹ฐ ํ‚ค๋กœ, ๊ฐ ๋ฌธ์ž๋ฅผ ํ”„๋กœํผํ‹ฐ ๊ฐ’์œผ๋กœ ๊ฐ€์ง€๋ฏ€๋กœ String ๋ž˜ํผ ๊ฐ์ฒด๋Š” ์œ ์‚ฌ ๋ฐฐ์—ด ๊ฐ์ฒด๋‹ค.

32.3 String ๋ฉ”์„œ๋“œ

String ๊ฐ์ฒด์—๋Š” ์›๋ณธ String ๋ž˜ํผ ๊ฐ์ฒด๋ฅผ ์ง์ ‘ ๋ณ€๊ฒฝํ•˜๋Š” ๋ฉ”์„œ๋“œ๊ฐ€ ์กด์žฌํ•˜์ง€ ์•Š๋Š”๋‹ค. ๊ทธ๋ž˜์„œ String ๊ฐ์ฒด์˜ ๋ฉ”์„œ๋“œ๋Š” ์–ธ์ œ๋‚˜ ์ƒˆ๋กœ์šด ๋ฌธ์ž์—ด์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค. ๋ฌธ์ž์—ด์€ ๋ณ€๊ฒฝ ๋ถˆ๊ฐ€๋Šฅํ•œ ๊ฐ’(immutable)ํ•œ ์›์‹œ ๊ฐ’์ด๊ธฐ ๋•Œ๋ฌธ์— String ๋ž˜ํผ ๊ฐ์ฒด๋„ ์ฝ๊ธฐ ์ „์šฉ ๊ฐ์ฒด(read only)๋กœ ์ œ๊ณต๋œ๋‹ค.

๋งŒ์•ฝ String ๋ž˜ํผ ๊ฐ์ฒด๊ฐ€ ์ฝ๊ธฐ ์ „์šฉ ๊ฐ์ฒด๊ฐ€ ์•„๋‹ˆ๋ฉด, ๋ณ€๊ฒฝ๋œ String ๋ž˜ํผ ๊ฐ์ฒด๋ฅผ ๋ฌธ์ž์—ด๋กœ ๋˜๋Œ๋ฆด ๋•Œ ๋ฌธ์ž์—ด์ด ๋ณ€๊ฒฝ๋œ๋‹ค. ๋”ฐ๋ผ์„œ String ๊ฐ์ฒด์˜ ๋ชจ๋“  ๋ฉ”์„œ๋“œ๋Š” String ๋ž˜ํผ ๊ฐ์ฒด๋ฅผ ์ง์ ‘ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์—†๊ณ , String ๋ž˜ํผ ๊ฐ์ฒด์˜ ๋ฉ”์„œ๋“œ๋Š” ์–ธ์ œ๋‚˜ ์ƒˆ๋กœ์šด ๋ฌธ์ž์—ด์„ ์ƒ์„ฑํ•˜์—ฌ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

32.3.1 String.prototype.indexOf

indexOf ๋ฉ”์„œ๋“œ๋Š” ๋Œ€์ƒ ๋ฌธ์ž์—ด์—์„œ ์ธ์ˆ˜๋กœ ์ „๋‹ฌ๋ฐ›์€ ๋ฌธ์ž์—ด์„ ๊ฒ€์ƒ‰ํ•ด ์ฒซ ๋ฒˆ์งธ ์ธ๋ฑ์Šค๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค. ๊ฒ€์ƒ‰์— ์‹คํŒจํ•˜๋ฉด -1์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

const str = "hello world"

str.indexOf("l") // 2
str.indexOf("or") // 7
str.indexOf("x") // -1 => ๊ฒ€์ƒ‰ ์‹คํŒจ์‹œ -1 ์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

str.indexOf("l", 3) // 3 => 2๋ฒˆ์งธ ์ธ์ˆ˜๋กœ ๊ฒ€์ƒ‰์„ ์‹œ์ž‘ํ•  ์ธ๋ฑ์Šค๋ฅผ ์ „๋‹ฌํ•  ์ˆ˜ ์žˆ๋‹ค.

indexOf ๋ฉ”์„œ๋“œ๋Š” ๋Œ€์ƒ ๋ฌธ์ž์—ด์— ํŠน์ • ๋ฌธ์ž์—ด์ด ์กด์žฌํ•˜๋Š”์ง€ ํ™•์ธํ•  ๋•Œ ์œ ์šฉํ•˜๋‹ค.

ES6์—์„œ ๋„์ž…๋œ String.prototype.includes ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๊ฐ€๋…์„ฑ์ด ๋” ์ข‹๋‹ค.

32.3.2 String.prototype.search

๋Œ€์ƒ ๋ฌธ์ž์—ด์—์„œ ์ธ์ˆ˜๋กœ ์ „๋‹ฌ๋ฐ›์€ ์ •๊ทœ ํ‘œํ˜„์‹๊ณผ ๋งค์น˜ํ•˜๋Š” ๋ฌธ์ž์—ด์„ ๊ฒ€์ƒ‰ํ•ด ์ผ์น˜ํ•˜๋Š” ๋ฌธ์ž์—ด์˜ ์ธ๋ฑ์Šค๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค. ๊ฒ€์ƒ‰์— ์‹คํŒจํ•˜๋ฉด -1์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

const str = 'hello world'

str.search(/o/) // 4
str.search(/x/) // -1

32.3.3 String.prototype.includes

ES6์—์„œ ๋„์ž…๋œ includes ๋ฉ”์„œ๋“œ๋Š” ๋Œ€์ƒ ๋ฌธ์ž์—ด์— ์ธ์ˆ˜๋กœ ์ „๋‹ฌ๋ฐ›์€ ๋ฌธ์ž์—ด์ด ํฌํ•จ๋˜์–ด ์žˆ๋Š”์ง€ ํ™•์ธํ•˜์—ฌ ๊ทธ ๊ฒฐ๊ณผ๋ฅผ true ๋˜๋Š” false ๋กœ ๋ฐ˜ํ™˜ํ•œ๋‹ค. 2๋ฒˆ์งธ ์ธ์ˆ˜๋กœ ๊ฒ€์ƒ‰ ์‹œ์ž‘ ์ธ๋ฑ์Šค๋ฅผ ์ „๋‹ฌํ•  ์ˆ˜ ์žˆ๋‹ค.

const str = 'hello world'

str.includes('hello') // true
str.includes('wor') // true
str.includes('x') // false
str.includes() // false

// 2๋ฒˆ์งธ ์ธ์ˆ˜๋กœ ๊ฒ€์ƒ‰ ์‹œ์ž‘ ์ธ๋ฑ์Šค๋ฅผ ์ „๋‹ฌํ•  ์ˆ˜ ์žˆ๋‹ค.
str.includes('l', 3) // true
str.includes('h', 3) // false

32.3.4 String.prototype.startsWith

ES6์—์„œ ๋„์ž…๋œ startsWith ๋ฉ”์„œ๋“œ๋Š” ๋Œ€์ƒ ๋ฌธ์ž์—ด์ด ์ธ์ˆ˜๋กœ ์ „๋‹ฌ๋ฐ›์€ ๋ฌธ์ž์—ด๋กœ ์‹œ์ž‘ํ•˜๋Š”์ง€ ํ™•์ธํ•˜์—ฌ true ๋˜๋Š” false๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค. 2๋ฒˆ์งธ ์ธ์ˆ˜๋กœ ๊ฒ€์ƒ‰ ์‹œ์ž‘ ์ธ๋ฑ์Šค๋ฅผ ์ „๋‹ฌํ•  ์ˆ˜ ์žˆ๋‹ค.

const str = 'hello world'

str.startsWith("he") // true
str.startsWith('x') // false

str.startsWith(' ', 5) // true

32.3.5 String.prototype.endsWith

ES6์—์„œ ๋„์ž…๋œ endsWith ๋ฉ”์„œ๋“œ๋Š” ๋Œ€์ƒ ๋ฌธ์ž์—ด์ด ์ธ์ˆ˜๋กœ ์ „๋‹ฌ๋ฐ›์€ ๋ฌธ์ž์—ด๋กœ ๋๋‚˜๋Š”์ง€ ํ™•์ธํ•˜์—ฌ true ๋˜๋Š” false๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค. 2๋ฒˆ์งธ ์ธ์ˆ˜๋กœ ๊ฒ€์ƒ‰ํ•  ๋ฌธ์ž์—ด์˜ ๊ธธ์ด๋ฅผ ์ „๋‹ฌํ•  ์ˆ˜ ์žˆ๋‹ค.

const str = 'hello world'

str.endsWith("d") // true
str.endsWith("ld") // true
str.endsWith('lo', 5) // true

32.3.6 String.prototype.charAt

๋Œ€์ƒ ๋ฌธ์ž์—ด์—์„œ ์ธ์ˆ˜๋กœ ์ „๋‹ฌ๋ฐ›์€ ์ธ๋ฑ์Šค์— ์œ„์น˜ํ•œ ๋ฌธ์ž๋ฅผ ๊ฒ€์ƒ‰ํ•˜์—ฌ ๋ฐ˜ํ™˜ํ•œ๋‹ค. ์ธ๋ฑ์Šค๋Š” ๋ฌธ์ž์—ด์˜ ๋ฒ”์œ„ (0 ~ ๋ฌธ์ž์—ด ๊ธธ์ด-1) ์‚ฌ์ด์˜ ์ •์ˆ˜์—ฌ์•ผ ํ•œ๋‹ค. ์ธ๋ฑ์Šค๊ฐ€ ๋ฌธ์ž์—ด์˜ ๋ฒ”์œ„๋ฅผ ๋ฒ—์–ด๋‚œ ์ •์ˆ˜์ธ ๊ฒฝ์šฐ ๋นˆ ๋ฌธ์ž์—ด์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

const str = 'hello world'

for(let i = 0; i < str.length; i++){
    console.log(str.charAt(i)) // h e l l o   w o r l d
}

32.3.7 String.prototype.substring

๋Œ€์ƒ ๋ฌธ์ž์—ด์—์„œ ์ฒซ ๋ฒˆ์งธ ์ธ์ˆ˜๋กœ ์ „๋‹ฌ๋ฐ›์€ ์ธ๋ฑ์Šค์— ์œ„์น˜ํ•œ ๋ฌธ์ž๋ถ€ํ„ฐ ๋‘ ๋ฒˆ์งธ ์ธ์ˆ˜๋กœ ์ „๋‹ฌ๋ฐ›์€ ์ธ๋ฑ์Šค์˜ ์œ„์น˜ํ•œ ๋ฌธ์ž์˜ ๋ฐ”๋กœ ์ด์ „ ๋ฌธ์ž๊นŒ์ง€ ๋ถ€๋ถ„ ๋ฌธ์ž์—ด์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค. ๋‘ ๋ฒˆ์งธ ์ธ์ˆ˜๋Š” ์ƒ๋žต ๊ฐ€๋Šฅํ•˜๋ฉฐ, ์ด๋•Œ ์ฒซ ๋ฒˆ์งธ ์ธ์ˆ˜๋ถ€ํ„ฐ ๋งˆ์ง€๋ง‰ ๋ฌธ์ž๊นŒ์ง€ ๋ถ€๋ถ„ ๋ฌธ์ž์—ด์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

const str = 'hello world'

str.substring(1,4) // "ell"
str.substring(1) // "ello World"

// ์ฒซ ๋ฒˆ์งธ ์ธ์ˆ˜๋Š” ๋‘ ๋ฒˆ์งธ ์ธ์ˆ˜๋ณด๋‹ค ์ž‘์€ ์ •์ˆ˜์—ฌ์•ผ ํ•˜์ง€๋งŒ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ธ์ˆ˜๋ฅผ ์ „๋‹ฌํ•ด๋„ ์ •์ƒ๋™์ž‘ํ•œ๋‹ค.
str.substring(4, 1) // "ell" => ์ฒซ ๋ฒˆ์งธ ์ธ์ˆ˜ > ๋‘ ๋ฒˆ์งธ ์ธ์ˆ˜์ธ ๊ฒฝ์šฐ ๋‘ ์ธ์ˆ˜๋Š” ๊ตํ™˜๋œ๋‹ค.
str.substring(-2) // "hello world" => ์ธ์ˆ˜ < 0 or NaN ์ธ ๊ฒฝ์šฐ 0์œผ๋กœ ์ทจ๊ธ‰๋œ๋‹ค.
str.substring(1, 100) // "ello World" => ์ธ์ˆ˜ > str.length ์ธ ๊ฒฝ์šฐ ๋ฌธ์ž์—ด์˜ ๊ธธ์ด๋กœ ์ทจ๊ธ‰๋œ๋‹ค.
str.substring(20) // ""

indexOf ๋ฉ”์„œ๋“œ์™€ ํ•จ๊ป˜ ์‚ฌ์šฉํ•˜๋ฉด ํŠน์ • ๋ฌธ์ž์—ด์„ ๊ธฐ์ค€์œผ๋กœ ์•ž๋’ค์— ์œ„์น˜ํ•œ ๋ถ€๋ถ„ ๋ฌธ์ž์—ด์„ ์ทจํ•  ์ˆ˜ ์žˆ๋‹ค.

const str = 'hello world'
str.substring(0, str.indexOf(" ")) // "hello"
str.substring(str.indexOf(" ") + 1, str.length) // "world"

32.3.8 String.prototype.slice

substring ๋ฉ”์„œ๋“œ์™€ ๋™์ผํ•˜๊ฒŒ ์ž‘๋™ํ•œ๋‹ค. ๋‹จ, slice ๋ฉ”์„œ๋“œ์—๋Š” ์Œ์ˆ˜์ธ ์ธ์ˆ˜๋ฅผ ์ „๋‹ฌํ•ด ๋’ค์—์„œ๋ถ€ํ„ฐ ๋ฌธ์ž์—ด์„ ์ž๋ฅผ ์ˆ˜ ์žˆ๋‹ค.

const str = 'hello world'
str.slice(0, 5) // hello
str.slice(-5) //world

32.3.9 String.prototype.toUpperCase

๋Œ€์ƒ ๋ฌธ์ž์—ด์„ ๋ชจ๋‘ ๋Œ€๋ฌธ์ž๋กœ ๋ณ€๊ฒฝํ•œ ๋ฌธ์ž์—ด์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

const str = 'HEllo world'
str.toUpperCase() // HELLO WORLD

32.3.10 String.prototype.toLowerCase

๋Œ€์ƒ ๋ฌธ์ž์—ด์„ ๋ชจ๋‘ ์†Œ๋ฌธ์ž๋กœ ๋ณ€๊ฒฝํ•œ ๋ฌธ์ž์—ด์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

const str = 'HEllo world'
str.toLowerCase() // hello world

32.3.11 String.prototype.trim

๋Œ€์ƒ ๋ฌธ์ž์—ด ์•ž ๋’ค ๊ณต๋ฐฑ ๋ฌธ์ž๋ฅผ ์ œ๊ฑฐํ•œ ๋ฌธ์ž์—ด์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

const str = "    ahah   "
str.trim() // 'ahah'

trimStart, trimEnd ๋ฅผ ์‚ฌ์šฉํ•ด ์•ž ๋’ค ๊ณต๋ฐฑ์„ ์ œ๊ฑฐํ•  ์ˆ˜ ์žˆ๋‹ค.

32.3.12 String.prototype.repeat

ES6์—์„œ ๋„์ž…๋œ repeat ๋ฉ”์„œ๋“œ๋Š” ๋Œ€์ƒ ๋ฌธ์ž์—ด์„ ์ธ์ˆ˜๋กœ ์ „๋‹ฌ๋ฐ›์€ ์ •์ˆ˜๋งŒํผ ๋ฐ˜๋ณตํ•ด ์—ฐ๊ฒฐํ•œ ์ƒˆ๋กœ์šด ๋ฌธ์ž์—ด์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค. ์ธ์ˆ˜๋กœ ์ „๋‹ฌ๋ฐ›์€ ์ •์ˆ˜๊ฐ€ 0 ์ด๋ฉด ๋นˆ ๋ฌธ์ž์—ด์„ ๋ฐ˜ํ™˜ํ•˜๊ณ , ์Œ์ˆ˜์ด๋ฉด RangeError๋ฅผ ๋ฐœ์ƒ์‹œํ‚จ๋‹ค. ์ธ์ˆ˜๋ฅผ ์ƒ๋žตํ•˜๋ฉด ๊ธฐ๋ณธ๊ฐ’์€ 0์ด๋‹ค.

const str = 'abc'

str.repeat() // ''
str.repeat(0) // ''
str.repeat(1) // 'abc'
str.repeat(5) // 'abcabcabcabcabc'
str.repeat(5.5) // 'abcabcabcabcabc' 2.5 => 2
str.repeat(-1) // RangeError: Invalid count value

32.3.13 String.prototype.replace

๋Œ€์ƒ ๋ฌธ์ž์—ด์—์„œ ์ฒซ ๋ฒˆ์งธ ์ธ์ˆ˜๋กœ ์ „๋‹ฌ๋ฐ›์€ ๋ฌธ์ž์—ด ๋˜๋Š” ์ •๊ทœํ‘œํ˜„์‹์„ ๊ฒ€์ƒ‰ํ•ด ๋‘ ๋ฒˆ์งธ ์ธ์ˆ˜๋กœ ์ „๋‹ฌํ•œ ๋ฌธ์ž์—ด๋กœ ์น˜ํ™˜ํ•œ ๋ฌธ์ž์—ด์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

const str = 'hello world'
str.replace('world', 'Lee') // 'hello Lee'

// ๊ฒ€์ƒ‰๋œ ๋ฌธ์ž์—ด์ด ์—ฌ๋Ÿฌ๊ฐœ์ธ ๊ฒฝ์šฐ, ์ฒซ ๋ฒˆ์งธ ๊ฒ€์ƒ‰๋œ ๋ฌธ์ž์—ด๋งŒ ์น˜ํ™˜ํ•œ๋‹ค.
const str2 = 'hello world world'
str2.replace('world', 'Lee') // 'hello Lee world'

// ํŠน์ˆ˜ํ•œ ๊ต์ฒด ํŒจํ„ด
const str3 = 'hello world'
str3.replace('world', '<strong>$&</strong>') // $& = ๊ฒ€์ƒ‰๋œ ๋ฌธ์ž์—ด

// ์ •๊ทœ ํ‘œํ˜„์‹ ์ „๋‹ฌ ๊ฐ€๋Šฅ
const str4 = 'hello hello'
str4.replace(/hello/gi, 'Lee') // 'Lee Lee'

๋‘ ๋ฒˆ์งธ ์ธ์ˆ˜๋กœ ์น˜ํ™˜ ํ•จ์ˆ˜๋ฅผ ์ „๋‹ฌํ•  ์ˆ˜๋„ ์žˆ๋‹ค. ์ฒซ ๋ฒˆ์งธ ์ธ์ˆ˜๋กœ ์ „๋‹ฌํ•œ ๋ฌธ์ž์—ด ๋˜๋Š” ์ •๊ทœ ํ‘œํ˜„์‹์— ๋งค์น˜ํ•œ ๊ฒฐ๊ณผ๋ฅผ ๋‘ ๋ฒˆ์งธ ์ธ์ˆ˜์ธ ์น˜ํ™˜ ํ•จ์ˆ˜์˜ ์ธ์ˆ˜๋กœ ์ „๋‹ฌํ•˜๋ฉด์„œ ํ˜ธ์ถœํ•˜๊ณ , ์น˜ํ™˜ ํ•จ์ˆ˜๊ฐ€ ๋ฐ˜ํ™˜ํ•œ ๊ฒฐ๊ณผ์— ๋งค์น˜ ๊ฒฐ๊ณผ๋ฅผ ์น˜ํ™˜ํ•œ๋‹ค.

32.3.14 String.prototype.split

๋Œ€์ƒ ๋ฌธ์ž์—ด์—์„œ ์ฒซ ๋ฒˆ์งธ ์ธ์ˆ˜๋กœ ์ „๋‹ฌํ•œ ๋ฌธ์ž์—ด ๋˜๋Š” ์ •๊ทœ ํ‘œํ˜„์‹์„ ๊ฒ€์ƒ‰ํ•ด ๋ฌธ์ž์—ด์„ ๊ตฌ๋ถ„ํ•œ ํ›„ ๋ถ„๋ฆฌ๋œ ๊ฐ ๋ฌธ์ž์—ด๋กœ ์ด๋ฃจ์–ด์ง„ ๋ฐฐ์—ด์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค. ์ธ์ˆ˜๋กœ ๋นˆ ๋ฌธ์ž์—ด์„ ์ „๋‹ฌํ•˜๋ฉด ๊ฐ ๋ฌธ์ž๋ฅผ ๋ชจ๋‘ ๋ถ„๋ฆฌํ•˜๊ณ , ์ธ์ˆ˜๋ฅผ ์ƒ๋žตํ•˜๋ฉด ๋Œ€์ƒ ๋ฌธ์ž์—ด ์ „์ฒด๋ฅผ ๋‹จ์ผ ์š”์†Œ๋กœ ํ•˜๋Š” ๋ฐฐ์—ด์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

const str = 'How are you doing?'

str.split(' ') // [ 'How', 'are', 'you', 'doing?' ]
str.split(/\s/) // [ 'How', 'are', 'you', 'doing?' ]
str.split('') // [ 'H', 'o', 'w', ' ', 'a', 'r', 'e', ' ', 'y', 'o', 'u', ' ', 'd', 'o', 'i', 'n', 'g', '?' ]
str.split() // [ 'How are you doing?' ]

// ๋‘ ๋ฒˆ์งธ ์ธ์ˆ˜๋กœ ๋ฐฐ์—ด์˜ ๊ธธ์ด๋ฅผ ์ง€์ •ํ•  ์ˆ˜ ์žˆ๋‹ค.
str.split(' ', 3) // [ 'How', 'are', 'you' ]

Last updated

Was this helpful?