【问题标题】:How to compare two strings regardless of their capitalization?无论大小写如何,如何比较两个字符串?
【发布时间】:2017-09-04 09:47:24
【问题描述】:

我有:

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(data) 给出正确的结果

   <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 会这样做

【问题讨论】:

  • String#toLowerCase 不适合您?
  • @NinaScholz 更新问题
  • 请添加一些用例以明确您想要什么。
  • 它看起来更像是一个 ajax 问题,而不是字符串问题。它是否适用于准确的输入和搜索?

标签: javascript jquery


【解决方案1】:

同时隐藏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()) {

【讨论】:

  • 没关系,正在尝试,谢谢。我已经更新了问题,我的错,因为我不太清楚。
  • 为了安全起见可能还想修剪它们
  • 是的,但是你会搜索像 spacemission 这样的标题,但没有这样的标题
  • @rob.m 修剪只删除末尾的空格,而不是字符之间的空格
  • 也可以是 html 中的空格,而不仅仅是用户输入
【解决方案2】:

你可以这样做

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

左边将等于spacemission,与右边的表达式相同

它会首先删除单词之间的任何空格,然后转换为小写。删除空格是必要的,因为space missionspace mission 不同

【讨论】:

  • 是的,但是你会搜索像 spacemission 这样的标题,但没有这样的标题
【解决方案3】:

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

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

    }

【讨论】:

    【解决方案4】:

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

    【讨论】:

      【解决方案5】:

      使用jquery获取输入结果

      var string = jQuery("#datafetch h2 a").text()
      

      然后您可以尝试以下任何一种方法 -

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

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

      如果你对正则表达式没问题(注意正则表达式中的 i 选项):

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

      也有“匹配”:

      string.match(/STRING/i)
      

      当没有找到匹配时匹配返回空值

      【讨论】:

        【解决方案6】:

        接受的答案是正确的,它确实回答了我原来的问题,这就是我接受它的原因。但是,通过使用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();
                  }
                });
              });
            }
          });
        }
        

        【讨论】:

        • 如果您只是将响应包装在 $() 中并在其中工作,则无需 setTimeout
        • @charlietfl 我使用 .promise().done 解决了这个问题,我已经更新了这个答案
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多