The toWellFormed() method of the String class in JavaScript returns a string where all lone surrogates of the original string are replaced with the Unicode replacement character U+FFFD.
The syntax of String.toWellFormed() method is:
toWellFormed()
This toWellFormed() method of String returns a string where all lone surrogates of this string are replaced with the Unicode replacement character U+FFFD.
String.toWellFormed() returns value of type String
.
In JavaScript, the toWellFormed()
method replaces lone surrogates with the Unicode replacement character U+FFFD.
For example,
str
containing a lone surrogate '\uD800'
.toWellFormed()
method to replace the lone surrogate with U+FFFD.wellFormedStr
.wellFormedStr
to the console using the console.log()
method.const str = '\uD800';
const wellFormedStr = str.toWellFormed();
console.log(wellFormedStr);
�
In JavaScript, the toWellFormed()
method can replace multiple lone surrogates in a string.
For example,
str
containing multiple lone surrogates '\uD800\uDC00\uD800'
.toWellFormed()
method to replace the lone surrogates with U+FFFD.wellFormedStr
.wellFormedStr
to the console using the console.log()
method.const str = '\uD800\uDC00\uD800';
const wellFormedStr = str.toWellFormed();
console.log(wellFormedStr);
���
In JavaScript, the toWellFormed()
method does not alter a string that is already well-formed.
For example,
str
containing a well-formed Unicode string 'Hello \uD83D\uDE00'
.toWellFormed()
method.wellFormedStr
.wellFormedStr
to the console using the console.log()
method.const str = 'Hello \uD83D\uDE00';
const wellFormedStr = str.toWellFormed();
console.log(wellFormedStr);
Hello 😀
In this JavaScript tutorial, we learned about toWellFormed() method of String: the syntax and few working examples with output and detailed explanation for each example.