Soundex //JavaScript Repository
Description
Phonetic code of a word, useful to search words with similar sounds, however it was developed for the English language.
Created: 2005.11.01
Created: 2005.11.01
Code (Download)
//+ Jonas Raoni Soares Silva
//@ http://jsfromhell.com/string/soundex [rev. #1]
String.prototype.soundex = function(p){
var i, j, l, r, p = isNaN(p) ? 4 : p > 10 ? 10 : p < 4 ? 4 : p,
m = {BFPV: 1, CGJKQSXZ: 2, DT: 3, L: 4, MN: 5, R: 6},
r = (s = this.toUpperCase().replace(/[^A-Z]/g, "").split("")).splice(0, 1);
for(i = -1, l = s.length; ++i < l;)
for(j in m)
if(j.indexOf(s[i]) + 1 && r[r.length-1] != m[j] && r.push(m[j]))
break;
return r.length > p && (r.length = p), r.join("") + (new Array(p - r.length + 1)).join("0");
};
Example (Example)
<script type="text/javascript">
//<![CDATA[
document.write("Jonas Jones Matrix Maytrix".replace(/\w+/g, function(s){
return "\"" + s +"\".soundex() = " + s.soundex() + "<br />";
}));
//]]>
</script>
Help
- precision
- "precision" of the code, the minimum precision is 4 and the maximum is 10
Rank (Votes: 14)
4.21