Skip to content

Instantly share code, notes, and snippets.

@megasmack
Created May 11, 2022 18:32
Show Gist options
  • Save megasmack/3bee875cb16ee5f88775642c4452c246 to your computer and use it in GitHub Desktop.
Save megasmack/3bee875cb16ee5f88775642c4452c246 to your computer and use it in GitHub Desktop.
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