最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Comparing similar strings in jquery - Stack Overflow

programmeradmin4浏览0评论

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.

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.

Share Improve this question edited Feb 8, 2012 at 16:20 Kumsal Obuz 8255 silver badges14 bronze badges asked Feb 8, 2012 at 15:06 Wern AnchetaWern Ancheta 23.3k38 gold badges105 silver badges140 bronze badges 4
  • Can you go into further detail as to how you want the strings compared? – Paul Nikonowicz Commented Feb 8, 2012 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 Commented Feb 8, 2012 at 15:08
  • You could use str2.indexOf(str1) > -1 if you just wanted to check that str2 contains str1. – jabclab Commented Feb 8, 2012 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 Commented Feb 8, 2012 at 15:12
Add a comment  | 

6 Answers 6

Reset to default 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');
}

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.

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"/> 

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');
    }

look on

http://code.google.com/p/google-diff-match-patch/

the demo in

http://neil.fraser.name/software/diff_match_patch/svn/trunk/demos/demo_diff.html

You can check if the string is present or not by

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

发布评论

评论列表(0)

  1. 暂无评论