[JavaScript] replace() 메서드

Yuri Lee·2021년 6월 20일
0

replace()

  • replace() 메서드는 어떤 패턴에 일치하는 일부 또는 모든 부분이 교체된 새로운 문자열을 반환한다.
  • 그 패턴은 문자열이나 정규식(RegExp)이 될 수 있고, 교체 문자열은 문자열이나 모든 매치에 대해서 호출된 함수일 수 있다.

Uasge

const p = 'The quick brown fox jumps over the lazy dog. If the dog reacted, was it really lazy?';

console.log(p.replace('dog', 'monkey'));
// expected output: "The quick brown fox jumps over the lazy monkey. If the dog reacted, was it really lazy?"


const regex = /Dog/i;
console.log(p.replace(regex, 'ferret'));
// expected output: "The quick brown fox jumps over the lazy ferret. If the dog reacted, was it really lazy?"

https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Global_Objects/String/replace

profile
Step by step goes a long way ✨

0개의 댓글