Skip to content

Instantly share code, notes, and snippets.

@m-arrieta-r
Last active May 15, 2020 15:07
Show Gist options
  • Save m-arrieta-r/6d262c6d420e529ba92ea64ecb3c7f41 to your computer and use it in GitHub Desktop.
Save m-arrieta-r/6d262c6d420e529ba92ea64ecb3c7f41 to your computer and use it in GitHub Desktop.
Regex: remove underscore - add spaces before uppercase - remove double spaces - remove duplicate words
let str = "Hello__helloWorld__minor"
str = str.replace(/_/g, ' ').trim() // remove underscore
str = str.replace(/([A-Z])/g, ' $1') // add spaces before uppercase
str = str.replace(/ +/g, ' '); // remove double space
str = str.replace(/(\b\S+\b)(?=.*\1)/ig, '') // remove duplicate words https://stackoverflow.com/a/46364625
console.log(str.trim())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment