[String] replace() 특정문자를 치환

Sejung Seo·2022년 6월 19일
0

JavaScript

목록 보기
11/19
post-thumbnail

replace()

string.replace(searchValue찾을 문자열, newValue변경할 문자열)

replace() 함수는 문자열에서 searchValue(찾을 문자열)를 찾아서 newValue(변경할 문자열)로 치환합니다.

replace() 함수 사용하기

let str = 'apple, banana, orange, banana'
let replaced_str = str.replace('banana', 'tomato')
console.log('변경 전', str)			//결과값: apple, banana, orange, banana
console.log('변경 후', replaced_str)	//결과값: apple, tomato, orange, banana

banana라는 문자를 tomato로 치환해줍니다.
단, 첫번째 banana만 치열되고 두번째 banana는 치열되지 않습니다.

replace()함수는 대소문자를 구분합니다.

let str = 'Apple, Banana, Orange'
let replaced_str = str.replace('banana', 'tomato')
console.log('변경 전', str)			//결과값: apple, Banana, orange
console.log('변경 후', replaced_str)	//결과값: apple, Banana, orange
profile
공부하는 코린이 🌼

0개의 댓글