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

[JAVASCRIPT] ๋ฌธ์ž์—ด ๋ฉ”์„œ๋“œ : replace( ) / replaceAll( )

by _ํ† ๋งคํ†  2022. 8. 18.
728x90

์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ : ๋ฌธ์ž์—ด ๋ฉ”์„œ๋“œ : replace()

์ด๋ฒˆ ์‹œ๊ฐ„์—๋Š” ๋ฌธ์ž์—ด ๋ฉ”์„œ๋“œ์ธ replace()์— ๋Œ€ํ•ด ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค! ๐Ÿ˜


#1. replace( )

replace() ๋ฉ”์„œ๋“œ๋Š” String.prototype.replace()์˜ ์•ฝ์ž๋กœ ๋ฌธ์ž์—ด์„ ์น˜ํ™˜ํ•˜๊ฑฐ๋‚˜ ๋ณ€ํ™˜ ํ•  ๋•Œ ์‚ฌ์šฉํ•˜๋Š” ๋ฉ”์„œ๋“œ์ž…๋‹ˆ๋‹ค. ์ฆ‰ ๋ฌธ์ž์—ด์„ ๋ถ€๋ถ„ ๋ฌธ์ž์—ด๋กœ ๊ตฌ๋ถ„ํ•˜๊ณ  ๋ณ€ํ™˜ํ•˜์—ฌ ์ƒˆ๋กœ์šด ๋ฐฐ์—ด๋กœ ๋ฐ˜ํ™˜ํ•ด์ค๋‹ˆ๋‹ค.

! replace( ) ์‚ฌ์šฉ ๋ฐฉ๋ฒ• !
"๋ฌธ์ž์—ด"replace("์ฐพ์„ ๋ฌธ์ž์—ด", "๋ณ€๊ฒฝํ•  ๋ฌธ์ž์—ด");
"๋ฌธ์ž์—ด"replace("์ฐพ์„ ๋ฌธ์ž์—ด", "๋ณ€๊ฒฝํ•  ๋ฌธ์ž์—ด");
"๋ฌธ์ž์—ด"replace(์ •๊ทœ์‹);
"๋ฌธ์ž์—ด"replace(์ •๊ทœ์‹, "๋ณ€๊ฒฝํ•  ๋ฌธ์ž์—ด");

const str1 = "javascript reference";

const cirrentStr1 = str1.replace("javascript", "์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ"); // ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ reference
const cirrentStr2 = str1.replace("j", "J");     // Javascript reference
const cirrentStr3 = str1.replace("e", "E");     // javascript rEference : e๊ฐ€ ์—ฌ๋Ÿฌ๊ฐœ์ผ ๊ฒฝ์šฐ ์ฒ˜์Œ ๋ฌธ์ž๋งŒ E๋กœ ๋ณ€๊ฒฝ๋œ๋‹ค. ๋ชจ๋‘ ๋ณ€๊ฒฝ์‹œํ‚ค๋ ค๋ฉด replaceAll() ๋˜๋Š” ์ •๊ทœ์‹์ธ /g๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋œ๋‹ค.
const cirrentStr4 = str1.replace(/e/g, "E");    //javascript rEfErEncE : ์ •๊ทœ์‹ /๋ฌธ์ž/g ์‚ฌ์šฉํ•˜์—ฌ ๋ชจ๋‘ ๋ณ€๊ฒฝํ•˜๊ธฐ : /g -> ์ „์—ญ /i -> ๋Œ€์†Œ๋ฌธ์ž ๋ฌด์‹œ
const cirrentStr5 = str1.replace(/e/gi, "E");   //javascript rEfErEncE

//์ด๋ฏธ์ง€ ๋ช…์นญ ๋ฐ”๊พธ๊ธฐ : ์ถฉ๋Œ์ด ์ผ์–ด๋‚  ์ˆ˜ ์žˆ์œผ๋‹ˆ ๋ณ€๊ฒฝํ•  ๋•Œ ๋ฌธ์ž์—ด์„ ์ƒ์„ธํžˆ ์ ์Šต๋‹ˆ๋‹ค.
const str2 = "http://www.naver.com/img01.jpg";
const cirrentStr6 = str2.replace("img01.jpg", "img02.jpg"); //http://www.naver.com/img02.jpg


const str3 = "010-2000-1000";
const cirrentStr7 = str3.replace("-", "");      //0102000-1000
const cirrentStr8 = str3.replace(/-/g, ""); //01020001000 : ์ •๊ทœ์‹ ์‚ฌ์šฉ //
const cirrentStr9 = str3.replace(/-/g, " "); //010 2000 1000
const cirrentStr10 = str3.replace(/-/g, "โ˜…"); //010โ˜…2000โ˜…1000
const cirrentStr11 = str3.replace(/[1-9]/g, "โ˜…"); //0โ˜…0-โ˜…000-โ˜…000 : 0์„ ์ œ์™ธํ•œ ๋‚˜๋จธ์ง€ ์ˆซ์ž๋ฅผ โ˜…๋กœ ๋ฐ”๊พธ๊ธฐ : ์•Œ๊ณ ๋ฆฌ์ฆ˜์—์„œ ๋งŽ์ด ์‚ฌ์šฉ

! point ! ๋ฌธ์ž์—ด์—์„œ ๋ณ€๊ฒฝํ•˜๋ ค๋Š” ๋ฌธ์ž์—ด์ด ์—ฌ๋Ÿฌ๊ฐœ์ผ ๊ฒฝ์šฐ, ์ฒซ๋ฒˆ์งธ๋กœ ๋ฐœ๊ฒฌํ•œ ๋ฌธ์ž์—ด๋งŒ ๋ณ€ํ™˜ํ•ด์ค๋‹ˆ๋‹ค.
์ด๋•Œ ์ •๊ทœ์‹์ธ /g๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋ชจ๋“  ๋ฌธ์ž์—ด์„ ๋ณ€ํ™˜ํ•ด์ค๋‹ˆ๋‹ค.


#2. replaceAll( )

replaceAll() ๋ฉ”์„œ๋“œ๋Š” replace()์™€ ๋™์ผํ•˜์ง€๋งŒ ๋ณ€๊ฒฝํ•˜๋ ค๋Š” ๋ฌธ์ž์—ด์ด ์—ฌ๋Ÿฌ๊ฐœ์ธ ๊ฒฝ์šฐ ํ•ด๋‹นํ•˜๋Š” ๋ชจ๋“  ๋ฌธ์ž์—ด์„ ๋ณ€ํ™˜ํ•ด์ค€๋‹ค๋Š” ์ฐจ์ด์ ์ด ์žˆ์Šต๋‹ˆ๋‹ค!

! replaceAll( ) ์‚ฌ์šฉ ๋ฐฉ๋ฒ• !
"๋ฌธ์ž์—ด"replaceAll("์ฐพ์„ ๋ฌธ์ž์—ด", "๋ณ€๊ฒฝํ•  ๋ฌธ์ž์—ด");
"๋ฌธ์ž์—ด"replaceAll("์ฐพ์„ ๋ฌธ์ž์—ด", "๋ณ€๊ฒฝํ•  ๋ฌธ์ž์—ด");
"๋ฌธ์ž์—ด"replaceAll(์ •๊ทœ์‹);
"๋ฌธ์ž์—ด"replaceAll(์ •๊ทœ์‹, "๋ณ€๊ฒฝํ•  ๋ฌธ์ž์—ด");

const str1 = "javascript reference";
const cirrentStr4 = str1.replaceAll("e", "E");  // javascript rEfErEncE

const str3 = "010-2000-1000";
const cirrentStr9 = str3.replaceAll("-", "");   //01020001000

! point ! ๋ฌธ์ž์—ด์—์„œ ๋ณ€๊ฒฝํ•˜๋ ค๋Š” ๋ฌธ์ž์—ด์ด ์—ฌ๋Ÿฌ๊ฐœ์ผ ๊ฒฝ์šฐ, ๋ชจ๋“  ๋ฌธ์ž์—ด์„ ๋ณ€ํ™˜ํ•ด์ค๋‹ˆ๋‹ค.

728x90

๋Œ“๊ธ€


Lucky Charms Rainbow
js
html
css