String.prototype.toWellFormed()
Baseline
2023
Newly available
Since â¨October 2023â©, this feature works across the latest devices and browser versions. This feature might not work in older devices or browsers.
Stringê°ì toWellFormed() ë©ìëë ì´ ë¬¸ìì´ì 모ë ë¡ ìë¡ê²ì´í¸ê° ì ëì½ë ëì²´ 문ì U+FFFDë¡ ëì²´ë 문ìì´ì ë°íí©ëë¤.
구문
toWellFormed()
매ê°ë³ì
ìì.
ë°í ê°
ì´ ë¬¸ìì´ì ë³µì¬ë³¸ì¸ ì 문ìì´ë¡, 모ë ë¡ ìë¡ê²ì´í¸ê° ì ëì½ë ëì²´ 문ì U+FFFDë¡ ëì²´ë©ëë¤. strì´ ì íì±ë 문ìì´ì¸ ê²½ì°ìë ì 문ìì´ì´ ë°íë©ëë¤(기본ì ì¼ë¡ strì ë³µì¬ë³¸).
ì¤ëª
JavaScriptì 문ìì´ì UTF-16ì¼ë¡ ì¸ì½ë©ë©ëë¤. UTF-16 ì¸ì½ë©ìë ìë¡ê²ì´í¸ ìì´ë¼ë ê°ë ì´ ìì¼ë©°, ì´ë UTF-16 문ì, ì ëì½ë ì½ë í¬ì¸í¸ ë° ë¬¸ìì í´ë¬ì¤í° ì¹ì ì ìì¸í ìê°ëì´ ììµëë¤.
toWellFormed()ë ì´ ë¬¸ìì´ì ì½ë ë¨ì를 ë°ë³µíê³ ëª¨ë ë¡ ìë¡ê²ì´í¸ë¥¼ ì ëì½ë ëì²´ 문ì U+FFFD ï¿½ë¡ ëì²´í©ëë¤. ì´ë ê² íë©´ ë°íë 문ìì´ì´ ì¬ë°ë¥¸ íìì 문ìì´ì´ ëì´ encodeURIì ê°ì´ ì¬ë°ë¥¸ íìì 문ìì´ì 기ëíë í¨ììì ì¬ì©í ì ììµëë¤. ì¬ì©ì ì ì 구íê³¼ ë¹êµí ë toWellFormed()ë ìì§ì´ 문ìì´ì ë´ë¶ ííì ì§ì ì ê·¼í ì ìì¼ë¯ë¡ ë í¨ì¨ì ì
ëë¤.
ì못ë íìì 문ìì´ì´ TextEncoderì ê°ì í¹ì 문맥ìì ì¬ì©ëë ê²½ì° ëì¼í ëì²´ 문ì를 ì¬ì©íì¬ ìëì¼ë¡ ì¬ë°ë¥¸ íìì 문ìì´ë¡ ë³íë©ëë¤. ë¡ ìë¡ê²ì´í¸ê° ë ëë§ë ëë ëì²´ 문ì(ë´ë¶ì 물ìíê° ìë ë¤ì´ì몬ë)ë¡ë ë ëë§ë©ëë¤.
ìì
>toWellFormed() ì¬ì©í기
const strings = [
// ë¡ ìì ìë¡ê²ì´í¸
"ab\uD800",
"ab\uD800c",
// ë¡ íì ìë¡ê²ì´í¸
"\uDFFFab",
"c\uDFFFab",
// ì íì±ë 문ìì´
"abc",
"ab\uD83D\uDE04c",
];
for (const str of strings) {
console.log(str.toWellFormed());
}
// Logs:
// "ab�"
// "ab�c"
// "�ab"
// "c�ab"
// "abc"
// "abðc"
encodeURI()ìì ì¤ë¥ í¼í기
encodeURIë ì ë¬ë°ì 문ìì´ì´ ì íì±ë 문ìì´ì´ ìëë¼ë©´ ì¤ë¥ë¥¼ ë°ììíµëë¤. ì´ë toWellFormed()를 ì¬ì©í´ì ì íì±ë 문ìì´ë¡ ë³íí¨ì¼ë¡ì í¼í ì ììµëë¤.
const illFormed = "https://example.com/search?q=\uD800";
try {
encodeURI(illFormed);
} catch (e) {
console.log(e); // URIError: URI malformed
}
console.log(encodeURI(illFormed.toWellFormed())); // "https://example.com/search?q=%EF%BF%BD"
ëª ì¸ì
| Specification |
|---|
| ECMAScript® 2026 Language Specification > # sec-string.prototype.towellformed > |