Comparing similar strings in jquery

Wern Ancheta picture Wern Ancheta · Feb 8, 2012 · Viewed 27.8k times · Source

How do I compare similar strings in jquery?

<script src="../../js/jq.js"></script>
<script>
$(function(){
    var str1 = $.trim($('#str1').val().toUpperCase());
    var str2 = $.trim($('#str2').val().toUpperCase());
    if(str1==str2){
        console.log('yep');
    }
});
</script>
<input type="text" id="str1" value="One String"/>
<input type="text" id="str2" value="One String1"/>

Comparing "One String" and "One String1" is not going to work if I'm only checking if the two values are equal. Is there any way to do this in jquery? For example I only want to compare 90% of the string.

Answer

You can see if one is contained inside the other for example:

if (str1.indexOf(str2) >= 0 || str2.indexOf(str1) >= 0)
    console.log('yep');
}