728x90
반응형
replace() / replaceAll()
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
const cirrentStr4 = str1.replaceAll("e", "E"); //javascript rEfErEncE
const cirrentStr5 = str1.replace(/e/g, "E"); //javascript rEfErEncE
const cirrentStr6 = str1.replace(/e/gi, "E"); //javascript rEfErEncE
const str2 = "https://www.naver.com/img01.jpg";
const cirrentStr7 = str2.replace("img01.jpg", "img02.jpg");
const str3 = "010-2000-1000";
const cirrentStr8 = str3.replace("-", ""); //0102000-1000
const cirrentStr9 = str3.replaceAll("-", ""); //01020001000
const cirrentStr10 = str3.replaceAll(/-/g, ""); //01020001000
const cirrentStr11 = str3.replaceAll(/-/g, " "); //010 2000 1000
const cirrentStr12 = str3.replaceAll(/[1-9]/g, "*"); //0*0-*000-*000
728x90
반응형
'javascript' 카테고리의 다른 글
JavaScript trim() / trimStart() / trimEnd() (2) | 2022.08.17 |
---|---|
JavaScript concat() (2) | 2022.08.17 |
JavaScript split() (2) | 2022.08.17 |
JavaScript repeat() (2) | 2022.08.17 |
JavaScript padStart() / padEnd() (2) | 2022.08.17 |