Skip to content

Instantly share code, notes, and snippets.

@codeorelse
Last active January 31, 2017 08:43
Show Gist options
  • Save codeorelse/28c115ff568252bc1a4a3684d50630b2 to your computer and use it in GitHub Desktop.
Save codeorelse/28c115ff568252bc1a4a3684d50630b2 to your computer and use it in GitHub Desktop.
Break word instances to seperate words
var breakWordsIntoTwoWords = function(sentenceContainingWord, word) {
var regex = /hypotheek([^ ][-a-z]*)/gi;
while ((m = regex.exec(sentenceContainingWord)) !== null) {
if (m.index === regex.lastIndex) {
regex.lastIndex++;
}
// The result can be accessed through the `m`-variable.
var firstLetterCapital = word.substr(0,1).toUpperCase();
var searchForWord = m[0].substr(0,1) === firstLetterCapital ? firstLetterCapital + word.substr(1) : firstLetterCapital.toLowerCase() + word.substr(1);
sentenceContainingWord = sentenceContainingWord.replace(m[0], searchForWord + ' ' + m[0].split(searchForWord)[1]);
}
return sentenceContainingWord;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment