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

๋‹ค์Œ ์˜ˆ์ œ๋Š” ๋‘ ๊ฐœ์˜ ๋‹ค๋ฅธ ์ •๊ทœ์‹ ๊ฐ์ฒด๋กœ ๋ฌธ์ž์—ด์„ ๊ฒ€์ƒ‰ํ•˜์—ฌ ์„ฑ๊ณต์ ์ธ ๊ฒ€์ƒ‰(์–‘์ˆ˜)์™€ ์‹ฌํŒจํ•œ ๊ฒ€์ƒ‰(-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?