string.replace()
replace()
λ©μλλ μ΄λ€ pattern
μ μΌμΉνλ μΌλΆ λλ λͺ¨λ λΆλΆμ replacement
λ‘ λ체νμ¬ μλ‘μ΄ λ¬Έμμ΄μ λ°ννλ€. κ·Έ pattern
μ λ¬Έμμ΄μ΄λ μ κ·μ(RegExp
)μ΄ λ μ μμΌλ©° replacement
λ λ¬Έμμ΄μ΄λ κ° λ§€μΉμ λν΄μ νΈμΆλ ν¨μμΌ μ μλ€. pattern
μ΄ λ¬Έμμ΄μΌ κ²½μ°, μ΅μ΄λ‘ μΌμΉν λΆλΆλ§ λ체λλ€.
const p = 'Cats are the best! cats are cute!';
const regex = /cats/gi; // iλ λμλ¬Έμλ₯Ό λ°μ§μ§ μλλ€.
p.replace(regex, 'Dogs'); // "Dogs are the best! Dogs are cute!"
p.replace(regex, 'Hamsters'); // "Hamsters are the best! Hamsters are cute!"
Syntax
const newStr = str.replace(regexp\substr, newSubstr\function);
Parameters
regexp (pattern)
RegExp
μ κ·μ κ°μ²΄ λλ 리ν°λ΄
μΌμΉνλ νλͺ©μ newSubStr
λλ μ§μ λ ν¨μκ° λ°ν ν κ°μΌλ‘ λ체λλ€.
substr (pattern - λ¬Έμμ΄μΈ κ²½μ°)
newSubStr
λ‘ λ체 λ String
, μ κ·μμΌλ‘ ν΄μνμ§ μκ³ λ¨Όμ λμ¨ κ²λ§ λ°λλ€.
newSubStr (replacement)
λ§€κ°λ³μλ‘ μ§μ λ regexp
(pattern) λ substr
(pattern) λ¬Έμμ΄μ λ체νλ λ¬Έμ, μ¬λ¬κ°μ§ νΉμ replacement patternμ΄ μ§μλλ©°, "λ§€κ°λ³μκ° string
μΌλ‘ μ§μ " μ°Έκ³
function (replacement)
μ£Όμ΄μ§ regexp
λλ substr
λ‘ μΌμΉ νλͺ©μ λ체νλλ° μ¬μ©ν μ νμ λ¬Έμμ΄μ λ§λ€κΈ° μν΄ νΈμΆλλ ν¨μ
μ΄ ν¨μμ μ 곡λ μΈμλ "ν¨μ
λ₯Ό λ§€κ°λ³μλ‘ μ§μ "μ°Έκ³
Return value
μΌλΆ λλ μ 체 ν¨ν΄μ΄ λ체λ μ λ¬Έμμ΄
Description
μ΄ λ©μλλ νΈμΆλ String obejctλ₯Ό λ°κΎΈμ§ μλλ€. μλ‘μ΄ λ¬Έμμ΄μ λ°ννλ€.
μ 체λ₯Ό κ²μνκ³ λ³κ²½νλ©΄ g
νλκ·Έλ₯Ό μ¬μ©νλ€.
λ§€κ°λ³μκ° λ¬Έμμ΄λ‘ μ§μ λμμ λ
λ체 λ¬Έμμ΄μ λ€μκ³Ό κ°μ νΉμ κ΅μ²΄ ν¨ν΄μ ν¬ν¨ν μ μλ€.
ν¨ν΄
μ½μ
$$
"$" μ½μ
$&
λ§€μΉλ λΆλΆ λ¬Έμμ΄μ μ½μ
$`
λ§€μΉλ λΆλΆ λ¬Έμμ΄ μμ λ¨μ΄ λΆλΆμ μ½μ νλ€.
$'
λ§€μΉλ λΆλΆ λ¬Έμμ΄ μ§νμ λ¬Έμμ΄ λΆλΆμ μ½μ
$n
n
μ΄ 1μ΄μ 99μ΄νμ μ μλΌλ©΄, 첫λ²μ§Έ λ§€κ°λ³μλ‘ λκ²¨μ§ RegExp κ°μ²΄μμ μκ΄νΈλ‘ λ¬ΆμΈ n
λ²μ§Έμ λΆλΆ ννμμΌλ‘ λ§€μΉλ λ¬Έμμ΄μ μ½μ
νλ€. μΈλ±μ€κ° 1λΆν° μμλλ€.
$& μμ
const html = `<ul>
<li>κ³ μμ΄</li>
<li>κ°μμ§</li>
<li>κ³ μμ΄</li>
</ul>`;
const regex = /κ³ μμ΄/gi;
const newHtml = html.replace(regex, "<a href='#none'>$&</a>");
/*
"<ul>
<li><a href='#none'>κ³ μμ΄</a></li>
<li>κ°μμ§</li>
<li><a href='#none'>κ³ μμ΄</a></li>
</ul>"
*/
$`
const str = 'κ³ μμ΄1 κ°μμ§1 κ³ μμ΄2 κ°μμ§2 κ³ μμ΄3';
const regex = /κ°μμ§/gi;
const result = str.replace(regex, "$`μμ μλ ν
μ€νΈκ° 볡μ¬λλ€"); // νμ¬ ν
μ€νΈλ μμ λλ€.
// "κ³ μμ΄1 κ³ μμ΄1 μμ μλ ν
μ€νΈκ° 볡μ¬λλ€1 κ³ μμ΄2 κ³ μμ΄1 κ°μμ§1 κ³ μμ΄2 μμ μλ ν
μ€νΈκ° 볡μ¬λλ€2 κ³ μμ΄3"
$'
const str = 'κ³ μμ΄1 κ°μμ§1 κ³ μμ΄2 κ°μμ§2 κ³ μμ΄3';
const regex = /κ°μμ§/gi;
const result = str.replace(regex, "$'λ€μ μλ ν
μ€νΈκ° λΆλλ€.");
// "κ³ μμ΄1 1 κ³ μμ΄2 κ°μμ§2 κ³ μμ΄3λ€μ μλ ν
μ€νΈκ° λΆλλ€.1 κ³ μμ΄2 2 κ³ μμ΄3λ€μ μλ ν
μ€νΈκ° λΆλλ€.2 κ³ μμ΄3"
ν¨μλ₯Ό λ§€κ° λ³μλ‘ μ§μ
λλ²μ§Έ λ§€κ°λ³μκ° ν¨μλ‘ μ§μ λ μ μλ€. μΌμΉνλ κ²½μ° ν¨μκ° νΈμΆλλ€. ν¨μμ κ²°κ³Ό(λ°νκ°)κ° λ체 λ¬Έμμ΄λ‘ μ¬μ©λλ€.
(μ°Έκ³ : μ΄ κ²½μ° μμ μΈκΈ λ νΉμΆ κ΅μ²΄ ν¨ν΄μ μ μ©λμ§ μλλ€.)
λ§€κ°λ³μ π
μ΄λ¦
μ£Όμ΄μ§ κ°
match
λ§€μΉλ λ¬Έμμ΄(μμ $&
μ ν΄λΉ)
p1, p2, ...
replace() 첫λ²μ§Έ μΈμκ° RegExp
κ°μ²΄μΈ κ²½μ°, nλ²μ§Έ κ΄νΈμμ κ°μ³λ κ·Έλ£Ήμ λ¬Έμμ΄($1, $2μ λμ)μ΄λ€. μλ₯Ό λ€μ΄ /(\a+)(\b+)/
μ΄ μ£Όμ΄μ§λ€λ©΄ p1
μ \a+
κ³Ό λ§€μΉλκ³ p2
λ \b+ κ³Ό λ§€μΉλλ€.
offset
κ²μμ€μΈ μ 체 λ¬Έμμ΄ μ€μμ μΌμΉνλ λΆλΆ λ¬Έμμ΄μ μ€νμ μ΄λ€. (μλ₯Ό λ€μ΄, μ 체 λ¬Έμμ΄μ΄ 'abcd'μ΄κ³ μΌμΉνλ νμ λ¬Έμμ΄μ 'bc'μΈ κ²½μ° μΈμλ 1μ΄λ€.)
string
μ 체 λ¬Έμμ΄μ κ²μ¬νλ€.
μΈμμ μ νν κ°μλ 첫λ²μ§Έ μΈμκ° RegExpκ°μ²΄μΈμ§ μ¬λΆμ λ°λΌ λ¬λΌμ§λ©°, κ΄νΈλ‘ μ§μ λ λΆλΆμ κ°μμ λ°λΌ λ¬λΌμ§λ€.
λ€μ μμ λ newString
μ abc-12345-#$*%
λ‘ κ΅μ²΄νλ€.
function replacer(match, p1, p2, p3, offset, string) {
console.log('offset', offset); // offset 0 κ²μ μ€μΈ μ 체 λ¬Έμμ΄μ€μ μΌμΉκ° μμλλ λΆλΆ
console.log('string', string); // string abc12345#$*%
// p1λ μ«μκ° μλ κ², p2λ μ«μ, p3λ μ«μμ λ¬Έμκ° μλ κ²
return [p1, p2, p3].join(' - '); // abc - 12345 - #$*%
}
let newString = 'abc12345#$*%'.replace(/([^\d]*)(\d*)([^\w]*)/, replacer);
console.log(newString);
Examples
replace()μ μ κ·ννμ μ μ
iλ λμλ¬Έμλ₯Ό ꡬλΆνμ§ μλλ€.
const str = 'Cats are the best! cats are cute!';
const newStr = str.replace(/cats/i, 'dogs');
newStr; // "dogs are the best! cats are cute!"
globalκ³Ό ignoreμ replace()μμ μ¬μ©νκΈ°
global replaceλ μ κ·μμμλ§ μ¬μ©ν μ μλ€. μ μμΌλ‘ νμν μ μλ€.
const str = 'Cats are the best! cats are cute!';
const newStr = str.replace(/cats/gi, 'dogs');
newStr; // "dogs are the best! dogs are cute!"
λ¬Έμμ΄μμ λ¨μ΄ μΉν
λ체 ν μ€νΈμ κ²½μ° μ€ν¬λ¦½νΈλ $1, $2μ κ·Έλ£ΉμΊ‘μ³λ₯Ό μ¬μ©νλ€.
const re = /(\w+)\s(\w+)/;
const str = 'John Smith';
const result = str.replace(re, '$2, $1');
result; // "Smith, John"
μΌμΉνλ λ¬Έμλ₯Ό μμ νλ μΈλΌμΈ ν¨μ μ¬μ©
μ΄ μμμμλ λͺ¨λ λλ¬Έμλ μλ¬Έμλ‘ λ³νλκ³ , μΌμΉνλ μμΉ λ°λ‘ μμ νμ΄νμ΄ μ½μ λλ€.
μ¬κΈ°μ μ€μνκ²μ μΆκ° μμ μ μΌμΉνλ νλͺ©μ΄ λ체λμ΄ λ°ννκΈ° μ μ νμν κ²μ΄λ€.
λ체 ν¨μμμλ matchλ λΆλΆμ΄ ν¨μμ μΈμλ‘ λ€μ΄μ¨λ€. κ·Έλ¦¬κ³ κ·Έ μΈμλ₯Ό λμλ¬Έμ λ³ν λ°ν μ§μ μ νμ΄νμΌλ‘ μ°κ²°λλ€.
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 = 'borderTop'.replace(/[A-Z]/, '-'+ '$&'.toLowerCase());
// "border-Top" μλ¬Έμλ‘ λ³νλμ§ μμλ€.
λ¬Έμλ₯Ό ν¨ν΄μΌλ‘ μ¬μ©νκΈ° μ μ λ¨Όμ '$&'.toLowerCase() λ¬Έμμ΄ λ¦¬ν°λ΄λ‘ νκ°λκΈ° λλ¬Έμ΄λ€.
νμ¨λ₯Ό μμ¨ μ¨λλ‘ λ체
νμ¨ μ¨λ Fλ‘ λλλ μ«μμ΄μ΄μΌ νλ€. ν¨μλ Cλ‘ λλλ μμ¨λ‘ λλ €μ€λ€. μλ₯Ό λ€μ΄, μ λ ₯λλ μκ° 212FμΈ κ²½μ°, ν¨μλ 100Cλ₯Ό λλ €μ€λ€. μ λ ₯λλ μ«μ 0FμΈ κ²½μ°, ν¨μλ -17.77777777777778Cλ₯Ό λλ €μ€λ€.
μ κ· ννμ testλ μμμ μ«μκ° Fλ‘ λλλμ§ νμΈνλ€. νμ¨ μ¨λμ μλ ν¨μμ λλ²μ§Έ μΈμ p1μ ν΅ν΄ κ·Έ κΈ°λ₯μ μ‘μΈμ€ ν μ μλ€. f2c()ν¨μλ λ¬Έμμ΄μ μ λ¬λ°μ νμ¨λ₯Ό κΈ°μ€μΌλ‘ μμ¨ μ«μλ₯Ό μ€μ νλ€. κ·Έλ°λ€μ f2c() ν¨μλ μμ¨ μ«μλ₯Ό λ°ννλ€. μ΄ ν¨μλ Prelμ s///e ν¨μμ μ μ¬νλ€.
function f2c(x) {
function convert(str, p1, offset, s) {
return ((p1 - 32) * 5/9) + 'C';
}
let s = String(x);
let test = /(-?\d+(?:\.\d*)?)F\b/g;
return s.replace(test, convert);
}
f2c("212F"); // "100C"
Last updated
Was this helpful?