Javascript
CA
Fuzzy Scoring Regex Mayhem
[]
James Padolsey
) + '$' , 'i' ) ; } makeFuzzyRegex ( 'omg' ) ; // => /^(?:(^.)?(o)(.??))?(?:(^.)?(m)(.??))?(?:(^.)?(g)(.??
slice ( 1 ) ; // Get captures specifically var score = 0 ; for ( var i = 0 , l = captures.
match ) return 0 ; var captures = match.
) + '$' , 'i' ) ; } }Our first attempt isn’t too bad:var score = createFuzzyScorer ( 'omg' ) ; score ( 'omg' ) ; // => 30 score ( 'xOmg' ) ; // => 29 score ( '.o.m.g.'
Here are the results:var score = createFuzzyScorer ( 'omg' ) ; score ( 'omg' ) ; // => 1.99 score ( 'xOmg' ) ; // => 1.90 score ( 'om' ) ; // => 1.66 score ( '.o.m.g.'
['captures'
'fuzzy'
'f'
'padolsey'
'character'
'var'
'mayhem'
'return'
'string'
'mistake'
'scoring'
'match'
'omg'
'score'
'james'
'regex']