比较jquery中的类似字符串 [英] Comparing similar strings in jquery

查看:125
本文介绍了比较jquery中的类似字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何比较jquery中的类似字符串?

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

如果我只是检查比较一个字符串和一个字符串1是不行的如果两个值相等。有没有办法在jquery中这样做?例如,我只想比较90%的字符串。

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.

推荐答案

检查出来: http://jsfiddle.net/Sj5dE/ 您可以注释掉a,b块以查看字符串的相似性。它当然区分大小写。我希望这有帮助。由于您的示例讨论了比较两个相似的字符串,我认为字符串的开头很可能是相同的,因此我没有引入任何子字符串逻辑,但可以随意更改函数。

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.

这篇关于比较jquery中的类似字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆