๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Javascript

search / march / charAt ๋ฉ”์„œ๋“œ

by oranssy 2022. 8. 23.
728x90
๋ฐ˜์‘ํ˜•

โญ๏ธ ๋ฌธ์ž์—ด ๊ฐ์ฒด ( 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(๊ฒ€์ƒ‰๊ฐ’); "๋ฌธ์ž์—ด".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( ) ๋ฉ”์„œ๋“œ์˜ ํ˜•์‹

"๋ฌธ์ž์—ด".match(๊ฒ€์ƒ‰๊ฐ’); "๋ฌธ์ž์—ด".match(์ •๊ทœ์‹ ํ‘œํ˜„);
// 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(์œ„์น˜๊ฐ’);
// 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);  //       // ๋ฌธ์ž๊ฐ€ ์—†๊ธฐ ๋•Œ๋ฌธ์— ์•„๋ฌด ๊ฒƒ๋„ ์•ˆ ๋‚˜์˜ด
728x90
๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€