Skip to content

Instantly share code, notes, and snippets.

@megasmack
Created May 11, 2022 18:32
Embed
What would you like to do?
Remove orphans from text string

Remove orphans from text string

Take the last word of text string and replace the space before it with a non-breaking space.

/**
* Take the last word of text string and replace the space before it with a non-breaking space.
* @param {string} str
* @returns {string}
*/
const deOrphan = (str) => {
return str ? str.replace(/ (\S*)$/, '\u00A0$1') : '';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment