3

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.

Kumsal Obuz
  • 825
  • 5
  • 14
Wern Ancheta
  • 22,397
  • 38
  • 100
  • 139
  • Can you go into further detail as to how you want the strings compared? – Paul Nikonowicz Feb 08 '12 at 15:08
  • 1
    What do you mean "90% of the string"? That 90% of the characters are equal? Or perhaps that one is a substring of another? – Andrew Marshall Feb 08 '12 at 15:08
  • You could use `str2.indexOf(str1) > -1` if you just wanted to check that `str2` contains `str1`. – jabclab Feb 08 '12 at 15:10
  • @AndrewMarshall: I want to determine if the pattern of the first string is similar with the other. 90% of the characters might also work, I just want to reduce redundancy of data using jquery. – Wern Ancheta Feb 08 '12 at 15:12

6 Answers6

6

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

if (str1.indexOf(str2) >= 0 || str2.indexOf(str1) >= 0)
    console.log('yep');
}
Shadow The GPT Wizard
  • 66,030
  • 26
  • 140
  • 208
5

Check this out : http://jsfiddle.net/Sj5dE/ You can comment out a,b block to see the similitude of the strings. It's of course case-sensitive. I hope this helps. Since your example talked about comparing two similar strings, I thought it'd be most likely that the beginning of the strings are the same so I didn't introduce any substring logic but feel free to change the function.

Kumsal Obuz
  • 825
  • 5
  • 14
3

Try this it might work

    <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"/> 
Sanjay Goswami
  • 1,386
  • 6
  • 13
  • ive been wondering this for some time. I know `=` is for defining, `==` is used to check if its equal, `!=` is not equal but what is `===`? Couldnt find it on google last time lol – Teun Pronk Feb 08 '12 at 15:13
  • === is strict equality. 5==="5" will result false. Normally, it's possible to get away with type casting but if you want your values to have an exact data type as well as the value then you can use ===. This might help http://www.w3schools.com/js/js_comparisons.asp – Kumsal Obuz Feb 08 '12 at 15:18
1

In javascript you can use the substring to get the 90% of the string you would like to compare.

$(function(){
    var str1 = $.trim($('#str1').val().toUpperCase().substring(0, 10);
    var str2 = $.trim($('#str2').val().toUpperCase().substring(0, 10);
    if(str1==str2){
        console.log('yep');
    }
scottm
  • 27,829
  • 22
  • 107
  • 159
0

You can check if the string is present or not by

$(".className").replace(/(^|\s)yourTextHere\S+/g, " ");

Bipin Shilpakar
  • 188
  • 2
  • 9