string matching in javascript / regex -
what best way match 2 strings contain same phrase? example there way match following 2 strings:
st1 = 'jenissplendidicecreams' st2 = 'jenisicecream'
what proper regex match 2 strings?
you need build regexp looks this:
/.*j.*e.*n.*i.*s.*i.*c.*e.*c.*r.*e.*a.*m.*/
this regexp matches if string being tested includes original characters, in order, arbitrary additional characters in between.
we can build enough doing
function make_regexp(str) { var letters = str.split(''); letters.push(''), letters.unshift(''); return new regexp(letters.join('.*')); } > make_regexp('jenisicecream') < /.*j.*e.*n.*i.*s.*i.*c.*e.*c.*r.*e.*a.*m.*/
now test if second string matches:
> make_regexp('jenisicecream').test('jenissplendidicecreams') < true
Comments
Post a Comment