Movatterモバイル変換


[0]ホーム

URL:


  1. 개발자를 위한 웹 기술
  2. JavaScript
  3. JavaScript 참고서
  4. 표준 내장 객체
  5. String
  6. String.prototype.replaceAll()

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

View in EnglishAlways switch to English

String.prototype.replaceAll()

Baseline Widely available

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

replaceAll() 메서드는pattern의 모든 일치 항목이replacement로 대체된 새 문자열을 반환합니다.pattern은 문자열 또는RegExp일 수 있으며replacement는 각 일치 항목에 대해 호출되는 문자열 또는 함수일 수 있습니다. 원래 문자열은 변경되지 않습니다.

시도해 보기

const paragraph = "I think Ruth's dog is cuter than your dog!";console.log(paragraph.replaceAll("dog", "monkey"));// Expected output: "I think Ruth's monkey is cuter than your monkey!"// Global flag required when calling replaceAll with regexconst regex = /Dog/gi;console.log(paragraph.replaceAll(regex, "ferret"));// Expected output: "I think Ruth's ferret is cuter than your ferret!"

구문

js
replaceAll(pattern, replacement)

매개변수

pattern

문자열이거나Symbol.replace 메서드가 있는 객체일 수 있습니다. 일반적인 예로정규식이 있습니다.Symbol.replace 메서드가 없는 모든 값은 문자열로 강제 변환됩니다.

pattern이 정규식이면 전역(g) 플래그가 설정되어 있어야 합니다. 그렇지 않으면TypeError가 발생합니다.

replacement

문자열이거나 함수일 수 있습니다. 교체는String.prototype.replace()와 동일한 의미 체계를 갖습니다.

반환 값

패턴의 모든 일치 항목이 교체자로 대체된 새 문자열입니다.

예외

TypeError

pattern이 정규식 이지만, 전역(g) 플래그가 설정되지 않은 경우 발생합니다(flags 속성에 "g"가 포함되지 않음).

설명

이 메서드는 호출된 문자열 값을 변경하지 않고 새 문자열을 반환합니다.

replace()와 달리 이 메서드는 첫 번째 문자열뿐만 아니라 문자열의 모든 항목을 바꿉니다. 특수 문자를 이스케이프하지 않고RegExp() 생성자를 호출하면 의도하지 않게 의미 체계가 변경될 수 있으므로 문자열이 정적이라고 확신하지 못할 때 특히 유용합니다.

js
function unsafeRedactName(text, name) {  return text.replace(new RegExp(name, "g"), "[REDACTED]");}function safeRedactName(text, name) {  return text.replaceAll(name, "[REDACTED]");}const report =  "A hacker called ha.*er used special characters in their name to breach the system.";console.log(unsafeRedactName(report, "ha.*er")); // "A [REDACTED]s in their name to breach the system."console.log(safeRedactName(report, "ha.*er")); // "A hacker called [REDACTED] used special characters in their name to breach the system."

patternSymbol.replace 메서드(RegExp 객체 포함)가 있는 객체인 경우 대상 문자열과replacement를 인수로 하여 해당 메서드를 호출합니다. 반환 값은replaceAll()의 반환 값이고, 이 경우replaceAll()의 동작은@@replace 메서드에 의해 완전히 인코딩되므로replace()와 동일한 결과를 갖게 됩니다(정규식이 전역이라는 추가 입력 유효성 검사는 제외).

pattern이 빈 문자열인 경우엔split() 동작과 유사하게 모든 UTF-16 코드 단위 사이에 교체자가 삽입됩니다.

js
"xxx".replaceAll("", "_"); // "_x_x_x_"

regex 속성(특히sticky 플래그)이replaceAll()과 상호 작용하는 방식에 대한 자세한 내용은RegExp.prototype[@@replace]()를 참조하세요.

예제

replaceAll() 사용

js
"aabbcc".replaceAll("b", ".");// 'aa..cc'

비전역 정규식 오류

js
"aabbcc".replaceAll(/b/, ".");// TypeError: replaceAll must be called with a global RegExp

아래는 잘 작동합니다.

js
"aabbcc".replaceAll(/b/g, ".");("aa..cc");

명세서

Specification
ECMAScript® 2026 Language Specification
# sec-string.prototype.replaceall

브라우저 호환성

같이 보기

Help improve MDN

Learn how to contribute

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2025 Movatter.jp