string.search
search()
๋ฉ์๋๋ ์ ๊ท์๊ณผ String object ์ฌ์ด์ ์ผ์น์ ๋ํ ๊ฒ์์ ์คํํ๋ค.
const paragraph = 'The cat (Felis catus) is a domestic species of small carnivorous mammal.';
// ๋จ์ด๋ ๊ณต๋ฐฑ์ด ์๋ ๋ชจ๋ ๋ฌธ์
const regex = /[^\w\s]/g;
const result = paragraph.search(regex); // 8
paragraph.charAt(result); // "("
Syntax
str.search(regexp)
Parameters
regexp
์ ๊ทํํ์ ๊ฐ์ฒด
์ ๊ท์์ด ์๋ ๊ฐ์ฒด๊ฐ ์ ๋ฌ๋๋ฉด, ๊ทธ๊ฒ์ new RegExp(obj)๋ฅผ ์ด์ฉํ์ฌ RegExp์ผ๋ก ์๋ฌต์ ์ผ๋ก ๋ณํ๋๋ค.
Return value
์ฃผ์ด์ง ๋ฌธ์์ด๊ณผ ์ ๊ท์์ฌ์ด์ ๊ฐ์ฅ ์ฒซ ๋งค์น์ index์ด๋ค. ๋งค์น๋์ง ์๋๋ค๋ฉด -1์ return ํ๋ค.
Description
ํจํด์ด ์๋์ง ์ฌ๋ถ์ ๋ฌธ์์ด ๋ด์ index๋ฅผ ์๊ณ ์ถ๋ค๋ฉด search()
๋ฅผ ์ฌ์ฉํ๋ค. (์ค์ง ์กด์ฌ ์ ๋ฌด๋ง ์๊ณ ์ถ๋ค๋ฉด test() ๋ฉ์๋๋ฅผ ์ฌ์ฉํ์ฌ boolean๊ฐ์ ๋ฐํํ๋ค.) ๋ ๋ง์ ์ ๋ณด๋ฅผ ์ป์ง๋ง ๋๋ฆด ์ ์๋ match()๋ฅผ ์ฌ์ฉํ ์ ์๋ค. (exec()์ ์ ์ฌํ๋ค)
Examples
search() ์ฌ์ฉ
๋ค์ ์์ ๋ ๋ ๊ฐ์ ๋ค๋ฅธ ์ ๊ท์ ๊ฐ์ฒด๋ก ๋ฌธ์์ด์ ๊ฒ์ํ์ฌ ์ฑ๊ณต์ ์ธ ๊ฒ์(์์)์ ์ฌํจํ ๊ฒ์(-1)์ ํ์ํ๋ค.
const str = 'The cat (Felis catus) is a domestic species of small carnivorous mammal.';
const re = /[A-Z]/g;
const re2 = /[,]/g;
str.search(re); // 0
str.search(re2); // -1
Last updated
Was this helpful?