| [ Web Proxy ] |
| Viewing: https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Global_Objects/String/replace | [Back] [Original] |
Get to know MDN better
This page was translated from English by the community. Learn more and join the MDN Web Docs community.
This feature is well established and works across many devices and browser versions. Its been available across browsers since 2015 7.
String replace() pattern , replacement . pattern RegExp . replacement . pattern , . .
const paragraph = "I think Ruth's dog is cuter than your dog!";
console.log(paragraph.replace("Ruth's", "my"));
// Expected output: "I think my dog is cuter than your dog!"
const regex = /Dog/i;
console.log(paragraph.replace(regex, "ferret"));
// Expected output: "I think Ruth's ferret is cuter than your dog!"
replace(pattern, replacement)
pattern Symbol.replace . . Symbol.replace .
replacement.
, .
. .
. g replaceAll() .
pattern Symbol.replace (RegExp ) , replacement . replace() . replace() [Symbol.replace]() . , " " RegExp.prototype[Symbol.replace]() .
pattern .
"xxx".replace("", "_"); // "_xxx"
g replace() . ( sticky ) replace() RegExp.prototype[Symbol.replace]() .
.
$$ |
"$" . |
$& |
. |
$` |
. |
$' |
. |
$n |
n(1 ) . n 100 . |
$<Name> |
Name . |
$n $<Name> pattern RegExp . pattern . ( ) .
"foo".replace(/(f)/, "$2");
// "$2oo"; .
"foo".replace("f", "$1");
// "$1oo"; , .
"foo".replace(/(f)|(g)/, "$2");
// "oo"; .
. . ( ) .
: .
.
function replacer(match, p1, p2, /* , */ pN, offset, string, groups) {
return replacement;
}
.
match
$& .p1, p2, , pN
replace() ( ) n RegExp . ( $1, $2 .) pattern /(\a+)(\b+)/, p1 \a+ p2 \b+ . (: "abc".replace(/(a)|(b)/, replacer)), undefined .offset
'abcd' 'bc' 1 .string
groups
undefined) . pattern . RegExp , .
newString 'abc - 12345 - #$*%' .
function replacer(match, p1, p2, p3, offset, string) {
// p1 p2 , p3 .
return [p1, p2, p3].join(" - ");
}
const newString = "abc12345#$*%".replace(/([^\d]*)(\d*)([^\w]*)/, replacer);
console.log(newString); // abc - 12345 - #$*%
.
replace() / .
const str = "Twas the night before Xmas...";
const newstr = str.replace(/xmas/i, "Christmas");
console.log(newstr); // Twas the night before Christmas...
'Twas the night before Christmas...' .
. replace() 'apple' 'orange' .
const re = /apples/gi;
const str = "Apples are round, and apples are juicy.";
const newstr = str.replace(re, "oranges");
console.log(newstr); // oranges are round, and oranges are juicy.
'oranges are round, and oranges are juicy' .
const re = /(\w+)\s(\w+)/;
const str = "Maria Cruz";
const newstr = str.replace(re, "$2, $1");
console.log(newstr); // Cruz, Maria
'Cruz, Maria' .
. .
.
function styleHyphenFormat(propertyName) {
function upperToHyphenLower(match, offset, string) {
return (offset > 0 ? "-" : "") + match.toLowerCase();
}
return propertyName.replace(/[A-Z]/g, upperToHyphenLower);
}
styleHyphenFormat('borderTop') 'border-top' .
. toLowerCase() . toLowerCase() .
//
const newString = propertyName.replace(/[A-Z]/g, "-" + "$&".toLowerCase());
'$&'.toLowerCase() ( '$&' ).
. "F" . "C" . "212F" "100C" . "0F" "-17.777777777778C" .
test F . p1 . f2c() . f2c() . Perl s///e .
function f2c(x) {
function convert(str, p1, offset, s) {
return `${((p1 - 32) * 5) / 9}C`;
}
const s = String(x);
const test = /(-?\d+(?:\.\d*)?)F\b/g;
return s.replace(test, convert);
}
. replacer offset .
"abcd".replace(/(bc)/, (match, p1, offset) => `${match} (${offset}) `);
// "abc (1) d"
. , . offset, string . ID groups offset .
function addOffset(match, ...args) {
const offset = args.at(-2);
return `${match} (${offset}) `;
}
console.log("abcd".replace(/(bc)/, addOffset)); // "abc (1) d"
console.log("abcd".replace(/(?<group>bc)/, addOffset)); // "abc (abcd) d"
addOffset . args.at(-2) offset string .
groups string .
function addOffset(match, ...args) {
const hasNamedGroups = typeof args.at(-1) === "object";
const offset = hasNamedGroups ? args.at(-3) : args.at(-2);
return `${match} (${offset}) `;
}
console.log("abcd".replace(/(bc)/, addOffset)); // "abc (1) d"
console.log("abcd".replace(/(?<group>bc)/, addOffset)); // "abc (1) d"
| Specification |
|---|
| ECMAScript 2027 LanguageSpecification # sec-string.prototype.replace |
Symbol.replace core-js String.prototype.replace String.prototype.replaceAll()String.prototype.match()RegExp.prototype.exec()RegExp.prototype.test()Symbol.replaceRegExp.prototype[Symbol.replace]()This page was last modified on 2025 10 9 by MDN contributors.
StringString.prototype.anchor()String.prototype.at()String.prototype.big()String.prototype.blink()String.prototype.bold()String.prototype.charAt()String.prototype.charCodeAt()String.prototype.codePointAt()String.prototype.concat()String.prototype.endsWith()String.prototype.fixed()String.prototype.fontcolor()String.prototype.fontsize()String.prototype.includes()String.prototype.indexOf()String.prototype.isWellFormed()String.prototype.italics()String.prototype.lastIndexOf()String.prototype.link()String.prototype.localeCompare()String.prototype.match()matchAll()String.prototype.normalize()String.prototype.padEnd()String.prototype.padStart()String.prototype.repeat()String.prototype.replace()String.prototype.replaceAll()String.prototype.search()String.prototype.slice()String.prototype.small()String.prototype.split()String.prototype.startsWith()String.prototype.strike()String.prototype.sub()String.prototype.substr()String.prototype.substring()String.prototype.sup()String.prototype.toLocaleLowerCase()String.prototype.toLocaleUpperCase()String.prototype.toLowerCase()String.prototype.toString()String.prototype.toUpperCase()String.prototype.toWellFormed()String.prototype.trim()String.prototype.trimEnd()String.prototype.trimStart()String.prototype.valueOf()String.prototype[@@iterator]()Object/FunctionObject.prototype.__defineGetter__()Object.prototype.__defineSetter__()Object.prototype.__lookupGetter__()Object.prototype.__lookupSetter__()Object.prototype.hasOwnProperty()Object.prototype.isPrototypeOf()Object.prototype.propertyIsEnumerable()Object.prototype.toLocaleString()Object.prototype.toString()Object.prototype.valueOf()Your blueprint for a better internet.
Portions of this content are 19982026 by individual mozilla.org contributors. Content available under a Creative Commons license.
| Web Proxy Viewer | New URL | Original Page |