728x90
반응형
replace()/ replaceAll()
replaceAll() 메서드는 무자열을 부분 문자열로 구분하고 배열로 반환한다.
// "문자열". 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만 바뀜
const cirrentStr4 = str1.replaceAll("e", "E"); //javascript rEfErEncE e가 다 바뀐다.
const cirrentStr5 = str1.replace(/e/g, "E"); //javascript rEfErEncE e가 다 바뀐다.(g)여러개를 선택하다.
const cirrentStr6 = str1.replace(/e/gi, "E"); //javascript rEfErEncE e가 다 바뀐다.
const str2 = "https://www.naver.com/img01.jpg";
const cirrentStr7 = str2.replace("img01","img02");
const str3 = "010-2000-1000";
// 01020001000
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("-", "*"); //010*2000*1000
const cirrentStr13 = str3.replaceAll(/[1-9]/g, "*"); //0*0-*000-*000
728x90
'javascript' 카테고리의 다른 글
repeat (2) | 2022.08.17 |
---|---|
concat (2) | 2022.08.17 |
split (2) | 2022.08.17 |
소문자/대문자/공백 (2) | 2022.08.17 |
문자열 결합 / 템플릿 문자열 (2) | 2022.08.17 |
댓글