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

[JavaScript] ๋ฌธ์ž์—ด ๋ฉ”์„œ๋“œ - indexOf() & lastIndexOf() ์•Œ์•„๋ณด๊ธฐ

by ์ฝ”๋”ฉ๊ณต์ฑ… 2022. 8. 16.
๋ฐ˜์‘ํ˜•

๋ฌธ์ž์—ด ๋ฉ”์„œ๋“œ : indexOf() / lastIndexOf()

์ด๋ฒˆ ํฌ์ŠคํŒ…์—์„œ๋Š” ๋ฌธ์ž์—ด ๋ฉ”์„œ๋“œ ์ค‘ indexOf์™€ lastIndexOf์— ๋Œ€ํ•ด ๋‹ค๋ค„๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.


indexOf()

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

"๋ฌธ์ž์—ด".indexOf(๊ฒ€์ƒ‰๊ฐ’)
"๋ฌธ์ž์—ด".indexOf(๊ฒ€์ƒ‰๊ฐ’, ์œ„์น˜๊ฐ’)

์ฐพ๋Š” ๋ฌธ์ž์—ด์ด ์—†์œผ๋ฉด -1์„ ๋ฆฌํ„ด(์ถœ๋ ฅ)ํ•œ๋‹ค.
๋ฌธ์ž์—ด์„ ์ฐพ์„ ๋•Œ ๋Œ€์†Œ๋ฌธ์ž๋ฅผ ๊ตฌ๋ถ„ํ•œ๋‹ค.

์ฝ”๋“œ ๋ณด๊ธฐ

const str1 = "javascript reference";
const currentStr1 = str1.indexOf("javascript");         // 0 : ์‹œ์ž‘์œ„์น˜๋ฅผ ์˜๋ฏธํ•จ
const currentStr2 = str1.indexOf("reference");          // 11
const currentStr3 = str1.indexOf("j");                  // 0
const currentStr4 = str1.indexOf("a");                  // 1
const currentStr5 = str1.indexOf("v");                  // 2
const currentStr6 = str1.indexOf("jquery");             // -1 : ์—†๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ์˜๋ฏธํ•จ
const currentStr7 = str1.indexOf("b");                  // -1
const currentStr8 = str1.indexOf("javascript", 0);      // 0
const currentStr9 = str1.indexOf("javascript", 1);      // -1
const currentStr10 = str1.indexOf("reference", 0);      // 11 : reference๋Š” 11๋ถ€ํ„ฐ ์‹œ์ž‘ํ•˜๊ธฐ ๋•Œ๋ฌธ์— 0~11 ์ค‘ ์•„๋ฌด ์ˆซ์ž๋‚˜ ์ž…๋ ฅํ•ด๋„ 11 ์ถœ๋ ฅ
const currentStr11 = str1.indexOf("reference", 1);      // 11
const currentStr12 = str1.indexOf("reference", 11);     // 11
const currentStr13 = str1.indexOf("reference", 12);     // -1

lastIndexOf()

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

"๋ฌธ์ž์—ด".lastIndexOf(๊ฒ€์ƒ‰๊ฐ’)
"๋ฌธ์ž์—ด".lastIndexOf(๊ฒ€์ƒ‰๊ฐ’, ์œ„์น˜๊ฐ’)

์ฐพ๋Š” ๋ฌธ์ž์—ด์ด ์—†์œผ๋ฉด -1์„ ๋ฆฌํ„ด(์ถœ๋ ฅ)ํ•œ๋‹ค.
๋ฌธ์ž์—ด์„ ์ฐพ์„ ๋•Œ ๋Œ€์†Œ๋ฌธ์ž๋ฅผ ๊ตฌ๋ถ„ํ•œ๋‹ค.

์ฝ”๋“œ ๋ณด๊ธฐ

const str1 = "javascript reference";
const currentStr14 = str1.lastIndexOf("javascript");        // 0
const currentStr15 = str1.lastIndexOf("reference");         // 11
const currentStr16 = str1.lastIndexOf("j");                 // 0
const currentStr17 = str1.lastIndexOf("a");                 // 3 : ๋’ค์—์„œ๋ถ€ํ„ฐ ์‹œ์ž‘ํ•ด์„œ ์ฒ˜์Œ ๋‚˜์˜ค๋Š” a๊ฐ€ 3๋ฒˆ์งธ ์ž๋ฆฌ์ž„
const currentStr18 = str1.lastIndexOf("v");                 // 2
const currentStr19 = str1.lastIndexOf("jquery");            // -1
const currentStr20 = str1.lastIndexOf("b");                 // -1
const currentStr21 = str1.lastIndexOf("javascript", 0);     // 0
const currentStr22 = str1.lastIndexOf("javascript", 1);     // 0
const currentStr23 = str1.lastIndexOf("reference", 0);      // -1
const currentStr24 = str1.lastIndexOf("reference", 1);      // -1
const currentStr25 = str1.lastIndexOf("reference", 11);     // 11
const currentStr26 = str1.lastIndexOf("reference", 12);     // 11
๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€


Reference Book

JavaScript
HTML
CSS
๊ด‘๊ณ  ์ค€๋น„์ค‘์ž…๋‹ˆ๋‹ค.