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 September 2015.

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® 2025 Language Specification
# sec-symbol.replace

브라우저 호환성

Report problems with this compatibility data on GitHub
desktopmobileserver
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
Deno
Node.js
replace

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support

같이 보기