This page was translated from English by the community. Learn more and join the MDN Web Docs community.

View in English Always switch to English

Symbol.replace

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since ⁨2020년 1월⁩.

Symbol.replace 정적 데이터 속성은 잘 알려진 심볼Symbol.replace를 나타냅니다. String.prototype.replace()String.prototype.replaceAll() 메서드는 현재 객체와 일치하는 문자열 일부를 대체하는 첫 번째 인수에서 이 심볼을 찾습니다.

자세한 내용은 RegExp.prototype[Symbol.replace](), String.prototype.replace()String.prototype.replaceAll()를 참고하시기 바랍니다.

시도해 보기

class Replace1 {
  constructor(value) {
    this.value = value;
  }
  [Symbol.replace](string) {
    return `s/${string}/${this.value}/g`;
  }
}

console.log("foo".replace(new Replace1("bar")));
// Expected output: "s/foo/bar/g"

잘 알려진 심볼 Symbol.replace.

Property attributes of Symbol.replace
쓰기 가능불가능
열거 가능불가능
설정 가능불가능

예제

Symbol.replace 사용하기

js
class CustomReplacer {
  constructor(value) {
    this.value = value;
  }
  [Symbol.replace](string) {
    return string.replace(this.value, "#!@?");
  }
}

console.log("football".replace(new CustomReplacer("foo"))); // "#!@?tball"

명세서

Specification
ECMAScript® 2026 Language Specification
# sec-symbol.replace

브라우저 호환성

같이 보기