โญ๏ธ ๋ฌธ์์ด ๊ฐ์ฒด ( search / march / charAt ๋ฉ์๋ ) โญ๏ธ
01. ํ์ค ๋ด์ฅ ๊ฐ์ฒด์ ์ข ๋ฅ : ๋ฌธ์์ด ๊ฐ์ฒด
ํ์ค ๋ด์ฅ ๊ฐ์ฒด(Standard Built-in Object)๋ ์๋ฐ์คํฌ๋ฆฝํธ๊ฐ ๊ธฐ๋ณธ์ ์ผ๋ก ๊ฐ์ง๊ณ ์๋ ๊ฐ์ฒด๋ค์ ๋งํ๋ฉฐ, ๋ค๋ฅธ ๊ฐ์ฒด์ ๊ธฐ์ด๊ฐ ๋๋ ํต์ฌ์ ์ธ ๊ฐ์ฒด ์
๋๋ค.
๋ด์ฅ ๊ฐ์ฒด์ ์ข
๋ฅ์๋ Object, Fuction, String, Array, Math, Number, Event, Boolean, Data, RegExp ๋ฑ์ด ์์ต๋๋ค.
๊ทธ ์ค์์ ๋ฌธ์์ด ๊ฐ์ฒด(String Object)๋ ๋ฌธ์์ด์ ์ ์ฅ/๊ด๋ฆฌํ๊ธฐ ์ํด ์ฌ์ฉํ๋ ๊ฐ์ฒด๋ฅผ ์๋ฏธํฉ๋๋ค.
___ ์ฐธ๊ณ ํ๊ธฐ ___
`1 ๋ฌธ์์ด์ ์์ฑํ ๋๋ ''"" ๋ฐ์ดํ๋ฅผ ์ฌ์ฉํ๊ฑฐ๋, new ํค์๋๋ฅผ ์ด์ฉํฉ๋๋ค.
`2 ๋ฌธ์์ด ๊ฐ์ฒด๋ ๋ถ๋ณ์ฑ์ด ์์ผ๋ฉฐ, ์ด๋ก์จ ๋ฉ๋ชจ๋ฆฌ ๊ณต๊ฐ์ ์ ์ฝ๊ณผ ๋ณด์์ฑ ๋ฐ ๋์์ฑ์ ํน์ง๋ ํจ๊ป ๊ฐ์ต๋๋ค.
02. search ๋ฉ์๋
๋ฌธ์์ด์์ ํน์ ๋ฌธ์์ ์์น๋ฅผ ์ฐพ๊ณ , ์ฐพ์ ๋ฌธ์ ๋๋ ํจํด์ ์ฒซ๋ฒ์งธ ์์น์ index ๊ฐ์ ๋ฐํํฉ๋๋ค.
indexOf( )์ ๊ฐ์ ๊ธฐ๋ฅ์ ํ์ง๋ง, search ๋ฉ์๋๋ ์ ๊ท์ ํํ์ด ๊ฐ๋ฅํ๋ค๋ ์ ์ด ๋ค๋ฆ
๋๋ค.
[1] search( ) ๋ฉ์๋์ ํ์
// search( ) ๋ฉ์๋์ ๋ฆฌํด ----------------------------------------------------------------
const str1 = "javascript reference";
const currentStr1 = str1.search("javascript"); // 0
const currentStr2 = str1.search("reference"); // 11
const currentStr3 = str1.search("j"); // 0
const currentStr4 = str1.search("a"); // 1
const currentStr5 = str1.search("v"); // 2
const currentStr6 = str1.search("jquery"); // -1 ๋ฐ์ดํฐ๊ฐ ์์ผ๋ฉด -1์ ๊ฐ์ด ๋์ด
const currentStr7 = str1.search("b"); // -1
const currentStr8 = str1.search(/[a-z]/g); // 0 // j์ ์์น๊ฐ์ ๋ฐํ. ์ ๊ท์ ํํ์ ์ฐพ์ ์ ์์
03. march ๋ฉ์๋
๋ฌธ์์ด์์ ํน์ ๋ฌธ์๋ฅผ ์ฐพ๊ณ , ์ฐพ์ ๋ฌธ์ ๋๋ ํจํด์ ๋ฐฐ์ด์ ๋ฐํํฉ๋๋ค.
[1] march( ) ๋ฉ์๋์ ํ์
// march( ) ๋ฉ์๋์ ๋ฆฌํด ----------------------------------------------------------------
const str1 = "javascript reference";
const currentStr1 = str1.match("javascript"); // [javascript]
const currentStr2 = str1.match("reference"); // [reference]
const currentStr3 = str1.match("r"); // [r]
const currentStr4 = str1.match(/reference/); // [reference]
const currentStr5 = str1.match(/Reference/); // null // ๋์๋ฌธ์๋ฅผ ๊ตฌ๋ถํจ
const currentStr6 = str1.match(/Reference/i); // [reference] // i ํ๋๊ทธ๋ก ๋์๋ฌธ์ ๊ตฌ๋ถํ์ง ์๋๋ก ์ค์
const currentStr7 = str1.match(/r/i); // ['r', 'r', 'r']
const currentStr8 = str1.match(/e/i); // ['e', 'e', 'e', 'e']
04. charAt ๋ฉ์๋
charAt ๋ฉ์๋๋ ๋ฌธ์์ด์ n ๋ฒ์งธ ๋ฌธ์๋ฅผ ๋ฐํํฉ๋๋ค.
[1] charAt( ) ๋ฉ์๋์ ํ์
// charAt( ) ๋ฉ์๋์ ๋ฆฌํด ----------------------------------------------------------------
const str1 = "javascript reference";
const currentStr1 = str1.charAt(); // j
const currentStr2 = str1.charAt(0); // j // 0๋ฒ์งธ ๋ฌธ์์ธ j๋ฅผ ๋ฐํ
const currentStr3 = str1.charAt(1); // a // 1๋ฒ์งธ ๋ฌธ์์ธ a๋ฅผ ๋ฐํ
const currentStr4 = str1.charAt(2); // v
const currentStr5 = str1.charAt(3); // a
const currentStr6 = str1.charAt(4); // s
const currentStr7 = str1.charAt(5); // c
const currentStr8 = str1.charAt(6); // r
const currentStr9 = str1.charAt(7); // i
const currentStr10 = str1.charAt(8); // p
const currentStr11 = str1.charAt(9); // t
const currentStr12 = str1.charAt(10); // ' '(๋น ์นธ)
const currentStr13 = str1.charAt(11); // r
const currentStr14 = str1.charAt(12); // e
const currentStr15 = str1.charAt(13); // f
const currentStr16 = str1.charAt(14); // e
const currentStr17 = str1.charAt(15); // r
const currentStr18 = str1.charAt(16); // e
const currentStr19 = str1.charAt(17); // n
const currentStr20 = str1.charAt(18); // c
const currentStr21 = str1.charAt(19); // e // ๋ฌธ์์ด์ ๋งจ ๋
const currentStr22 = str1.charAt(20); // // ๋ฌธ์๊ฐ ์๊ธฐ ๋๋ฌธ์ ์๋ฌด ๊ฒ๋ ์ ๋์ด
'Javascript' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
ํฌ๊ธฐ์ ์์น์ ๋ํ ์์ฑ (5) | 2022.09.01 |
---|---|
GSAP (3) | 2022.08.29 |
ํจ์์ ์ ํ (2) | 2022.08.23 |
๋ฌธ์์ด ๊ฐ์ฒด (9) includes ๋ฉ์๋ (3) | 2022.08.18 |
๋ฌธ์์ด ๊ฐ์ฒด (8) indexOf / lastindexOf ๋ฉ์๋ (2) | 2022.08.18 |
๋๊ธ