[ Web Proxy ]
URL:
Viewing: https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Global_Objects/String/toWellFormed [Back]  [Original]

String.prototype.toWellFormed() - JavaScript | MDN

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

View in English Always switch to English

String.prototype.toWellFormed()

Baseline Widely available

This feature is well established and works across many devices and browser versions. Its been available across browsers since 2023 10.

String toWellFormed() U+FFFD .

In this article

js
toWellFormed()

.

, U+FFFD . str ( str ).

JavaScript UTF-16 . UTF-16 , UTF-16 , .

toWellFormed() U+FFFD . encodeURI . toWellFormed() .

TextEncoder . ( ) .

toWellFormed()

js
const strings = [
  //   
  "ab\uD800",
  "ab\uD800c",
  //   
  "\uDFFFab",
  "c\uDFFFab",
  //   
  "abc",
  "ab\uD83D\uDE04c",
];

for (const str of strings) {
  console.log(str.toWellFormed());
}
// Logs:
// "ab"
// "abc"
// "ab"
// "cab"
// "abc"
// "abc"

encodeURI()

encodeURI . toWellFormed() .

js
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 2027 LanguageSpecification
# sec-string.prototype.towellformed


Web Proxy Viewer  |  New URL  |  Original Page