【问题标题】:In jQuery how do you save a specific wildcard string to variable?在 jQuery 中,如何将特定的通配符字符串保存到变量中?
【发布时间】:2012-02-06 12:11:11
【问题描述】:

我想从现有字符串中删除通配符字符串,然后将其附加到 div 之后。

在我的例子中,我想从一个字符串中找到一个零件号,然后将该字符串设置为一个变量。

所以不是这个(请注意 [] 之间可以是任何东西):

<div> The part number is [8F8S0JJ9]</div>
<div> The second part number is [V9S7D73]</div>

我会的:

<div> The part number is</div>
[8F8S0JJ9]
<div> The second part number is</div>
[V9S7D73]

这是我的代码:

// Get the product number that lies between [ ] marks from all div elements
$('div:contains('['+*+']')').html(function() { 

//Look for the wildcard string and save it to a variable. how can I search within the string?!
        var $finalstring = $(this).search('['+*+']');

//remove it from the string
$(this).replace($finalstring, '');

//Set it to display after the string
$(this).append($finalstring);
    });

谢谢

【问题讨论】:

  • 通配符字符串会一直是句子中的最后一个单词吗?

标签: jquery string append wildcard


【解决方案1】:

如果您不想使用 string.indexOf('')string.substring('') 的组合,请尝试在此答案中链接到以下插件:

jQuery selector regular expressions

【讨论】:

    【解决方案2】:

    首先,我相信你不能简单地使用 jQuery 选择器来做到这一点。因此,您需要构建自己的过滤器。使用简单的正则表达式,您可以获得相同的效果。其次,您需要删除现有内容。最后,您需要在原始 div 之后插入此内容。你有一些不正确的语法,但这里有一种方法:

    http://jsfiddle.net/zbE2F/

    $("div").filter(function(index){
        return /\[.*\]/.test($(this).html());
    }).each(function() { 
        var $finalstring = $(this).html().match(/\[.*\]/)[0];
        $(this).html($(this).html().replace(/\[.*\]/),"");
        $("<div></div>").insertAfter(this).append($finalstring);
    });
    

    希望这能让你更接近你所需要的,但请注意,如果任何时候一个 div 包含两个目标字符串,你就会遇到问题。

    【讨论】:

      【解决方案3】:

      我会添加我的解决方案,即使它与约瑟夫的基本相同哈哈。

      http://jsfiddle.net/PjdXr/

      var reg = /\[.*\]/;
      $("div").filter(function(){
          return $(this).text().match(reg);  
      }).html(function(i,h) { 
          var nr = h.match(reg);
          $(this).after(nr[0]);
          return h.replace(reg,'');
      });
      

      【讨论】:

        【解决方案4】:

        这是一个可能的解决方案,它假设通配符始终是句子中的最后一个单词:

        http://jsfiddle.net/Aq9qe/

        <div class='str'>Here is the [wildcard]</div>
        
        var str = $('.str').html();
        var split = str.split(' ');
        
        var wild = split.pop();
        
        $('.str').html(str.replace(wild, ' '));
        $('.str').after(wild);
        

        【讨论】:

          【解决方案5】:

          我会简化事情并只使用一个类:

          $('.part').each(function(){
              var $this       = $(this), 
                  partnum_reg = /(\[(.)+\])/g,
                  partnum     = String($this.html().match(partnum_reg));
          
              $this.html($this.html().replace(partnum, ''));
              $('<div/>').insertAfter($this).html(partnum);
          });
          

          演示:http://jsfiddle.net/hB4Zp/

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2011-03-16
            • 1970-01-01
            • 2014-07-17
            • 2015-07-07
            • 1970-01-01
            • 2019-04-19
            • 1970-01-01
            • 2019-01-05
            相关资源
            最近更新 更多