Skip to content

Instantly share code, notes, and snippets.

@CodeSigils
Created October 14, 2016 08:06
Show Gist options
  • Save CodeSigils/8cf826125f29c1619336e0ef43f57aaa to your computer and use it in GitHub Desktop.
Save CodeSigils/8cf826125f29c1619336e0ef43f57aaa to your computer and use it in GitHub Desktop.
Typo corrector
function typoCorrector(sourceText, target, value) {
// var matchPattern = target.match(/[A-Za-z]/g);
var matchPattern = new RegExp(target, 'g');
var matchCount = (sourceText.match(matchPattern) || []).length;
console.log(
"Replacing " + matchCount + " instances of " + target + " with " + value);
return sourceText.replace(matchPattern, value);
}
// tests
function testTypoCorrector() {
var sourceText = 'cats dogs cats dogs';
var target = 'cats';
var value = 'dogs';
var expected = 'dogs dogs dogs dogs';
if (typoCorrector(sourceText, target, value) === expected) {
console.log('SUCCESS: `typoCorrector` is working');
}
else {
console.log('FAILURE: `typoCorrector` is not working');
}
}
testTypoCorrector();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment