如何比较两个字符串而不管它们的大小写?

时间:2017-04-09 08:35:38

标签: javascript jquery

我有:

mytext = jQuery('#usp-title').val();

然后我做

if(jQuery("#datafetch h2 a").text() == mytext) {

但有一个标题可能是:

Space Missionspace mission

一样

由于其他原因,我无法将文本格式标准化,因此我真的在考虑比较两个字符串而不管它们的大小写。

更新

问题是我使用输入字段作为搜索,因此用户可以键入space mission,而帖子标题将是Space mission,因此搜索结果不准确,我需要确切的短语。

完整代码:

<div id="datafetch"></div>

function fetch(){
  jQuery('#datafetch').empty();
  mytext = jQuery('#usp-title').val();
    jQuery.ajax({
        url: '<?php echo admin_url('admin-ajax.php'); ?>',
        type: 'post',
        data: { action: 'data_fetch', exactwords:  mytext },
        success: function(data) {
            jQuery('#datafetch').html( data );
        if(jQuery("#datafetch h2 a").text().toLowerCase().trim() == mytext.toLowerCase().trim()) {
            jQuery("#componi").attr("disabled", "disabled").hide();
        } else {
                jQuery("#componi").removeAttr("disabled", "disabled").show();
        }
            if(jQuery("#datafetch h2 a").text() != mytext) {
            jQuery("#fatto").hide();
            jQuery('#datafetch').empty();
        }
        }
    });

}
实际上它更棘手,因为搜索来自文本输入,所以关于ajax的查询我认为不仅仅是大写,只是一个想法。上面的最新代码没有输入结果:

space mission,而帖子标题为Space mission

更新2

发表文章标题:

Space Mission

用户搜索

space mission

Console.log(数据)给出了正确的结果

   <ul class="list-unstyled margin-top-80">
       <li>
            <h2><a target="_blank" href="example.com"">Space Mission</a></h2>
       </li>
   </ul>

然而,这并没有发生:

jQuery('#datafetch').html( data );

如果我插入正确的单词Space Mission,那么

6 个答案:

答案 0 :(得分:6)

隐藏toLowercase()

"Space Mission".toLowerCase() == "space mission".toLowerCase()

在你的问题的背景下

if(jQuery("#datafetch h2 a").text().toLowerCase() == mytext.toLowerCase()) {

根据@charlietfl

的建议,在用户输入尾随空格或前导空格的情况下进行修剪
if(jQuery("#datafetch h2 a").text().trim().toLowerCase() == mytext.trim().toLowerCase()) {

答案 1 :(得分:1)

使用jquery

获取输入结果
var string = jQuery("#datafetch h2 a").text()

然后您可以尝试以下任何一项 -

简单字符串比较:将两者转换为相同的大小写:

string.toUpperCase() === "String".toUpperCase()

如果您使用正则表达式(请注意正则表达式中的i选项):

var otherString = /String/i
otherString.test(string)

还有“匹配”:

string.match(/STRING/i)
找不到匹配项时

匹配返回空值

答案 2 :(得分:1)

接受的答案是正确的,它确实回答了我原来的问题,这就是我接受它的原因。但是,使用setTimeout(function()解决了这个问题,因为填充div的数据与检查2值比较之间存在一点延迟。

function fetch(){
        jQuery.ajax({
            url: '<?php echo admin_url('admin-ajax.php'); ?>',
            type: 'post',
            data: { action: 'data_fetch', exactwords:  jQuery('#usp-title').val() },
            success: function(data) {
                    jQuery("#datafetch").html(data);
                    setTimeout(function() {
                        var text1 = jQuery("#datafetch").find("h2").find("a").html();
                        var text1B = text1.toLowerCase();
                        var text2 = jQuery('#usp-title').val();
                        var text2B = text2.toLowerCase();
                        if(text1B == text2B) {
                            jQuery("#componi").attr("disabled", "disabled").hide();
                        } else {
                            jQuery("#componi").removeAttr("disabled", "disabled").show();
                            jQuery("#fatto").hide();
                            jQuery('#datafetch').empty();
                        }
                    }, 1000);
            }
        });
}

<强>更新

使用.promise().done()更好

function fetch(){
    var text1;
    var text1B;
    var text2;
    var text2B;
        jQuery.ajax({
            url: '<?php echo admin_url('admin-ajax.php'); ?>',
            type: 'post',
            data: { action: 'data_fetch', exactwords:  jQuery('#usp-title').val() },
            success: function(data) {
                    jQuery("#datafetch").html(data).promise().done(function(){
                        text1 = jQuery("#datafetch").find("h2").find("a").html();
                        text1B = text1.toLowerCase();
                        text2 = jQuery('#usp-title').val();
                        text2B = text2.toLowerCase();
                        if (text1B != text2B) {
                            jQuery("#componi").removeAttr("disabled", "disabled").show();
                            jQuery("#fatto").hide();
                            jQuery('#datafetch').empty();
                        } else if (text1B == text2B) {
                            jQuery("#componi").attr("disabled", "disabled").hide();
                        }
                    });
            }
        });
}

更新两次

这是我最后使用的

function fetch(){
  jQuery.ajax({
    url: '<?php echo admin_url('admin-ajax.php'); ?>',
    type: 'post',
    data: { action: 'data_fetch', exactwords:  jQuery('#usp-title').val() },
    success: function(data) {
      var text2;
      var text2B;
      text2 = jQuery('#usp-title').val();
      text2B = text2.toLowerCase();
      jQuery("#datafetch").html(data).promise().done(function(){
        jQuery("#datafetch ul li h2 a").each(function() {
          var $this = jQuery(this);
          if ($this.text().toLowerCase() !== text2B) {
            $this.parent().parent().hide();
          } else if (text1B == text2B) {
            jQuery("#componi").attr("disabled", "disabled").hide();
          }
        });
      });
    }
  });
}

答案 3 :(得分:0)

你可以这样做

"Space Mission".replace(/\s+/g, "").toLowerCase() == "space    mission".replace(/\s+/g, "").toLowerCase()

左侧将与右侧的表达式相同spacemission

即使在单词之间也会删除任何空白区域。将转换为小写。删除空格是必要的,因为space missionspace mission

不同

答案 4 :(得分:0)

您需要将两个字符串转换为小写或大写,然后进行比较。

    var mytext = jQuery('#usp-title').val();
    if(jQuery("#datafetch h2 a").text().toLowerCase() == mytext.toLowerCase()) 
    {

    }

答案 5 :(得分:0)

将两个字符串转换为大写或小写

相关问题