replace_JavaScript

miin·2021년 12월 28일
0

Java Script

목록 보기
15/35
post-thumbnail

replace()

문자 변경하기
1) 문자열에서 변경하려는 문자열이 여러번 반복될 경우, 첫번째로 발견한 문자열만 치환해준다, 대소문자 구별

let str = 'apple, banana, orange';
let replaced_str = str.replace('banana', 'tomato');
//'apple, tomato, orange'

2) 모든 문자열 치환, 대소문자 구별

let str = 'apple, banana, orange, banana';
let replaced_str = str.replace(/banana/g, 'tomato');
//'apple, tomato, orange, tomato'

3) 대소문자 구별없이 치환

let str = 'apple, Banana, orange';
let replaced_str = str.replace(/banana/i, 'tomato');
//apple, tomato, orange

0개의 댓글