diff --git a/packages/rich-text/src/get-text-content.js b/packages/rich-text/src/get-text-content.js index c1c23d5bccd75c..17fb064eb36d8e 100644 --- a/packages/rich-text/src/get-text-content.js +++ b/packages/rich-text/src/get-text-content.js @@ -8,6 +8,11 @@ import { /** @typedef {import('./create').RichTextValue} RichTextValue */ +const pattern = new RegExp( + `[${ OBJECT_REPLACEMENT_CHARACTER }${ LINE_SEPARATOR }]`, + 'g' +); + /** * Get the textual content of a Rich Text value. This is similar to * `Element.textContent`. @@ -17,7 +22,7 @@ import { * @return {string} The text content. */ export function getTextContent( { text } ) { - return text - .replace( new RegExp( OBJECT_REPLACEMENT_CHARACTER, 'g' ), '' ) - .replace( new RegExp( LINE_SEPARATOR, 'g' ), '\n' ); + return text.replace( pattern, ( c ) => + c === OBJECT_REPLACEMENT_CHARACTER ? '' : '\n' + ); }