如何从容器中获取完整字符串?

时间:2012-03-15 10:25:24

标签: javascript jquery

我想从div搜索特定字符串并找出整个(完整)字符串

I.e:

  <div id='MSO_ContentTable'> 

      [Rotator(Cat=Marketing,Top=5,AutoRotate=true)]

      [Rotator(sg=dummy,Bottom=5,Autosb=true)] 

  blah blah blah .......

  </div>

div包含许多数据,但我只想在Div内容中使用Rotator字符查找完整字符串。

    $("div[id*='MSO_ContentTable']:contains('[Rotator')").each(function()
    {
       // find out div but not getting full string.
       //.In div also contains another html some thing like
       //<table>,<div> etc. i want to just find string which contain(Rotator)

    });

2 个答案:

答案 0 :(得分:1)

你可以这样(你可能需要根据你的实际内容进行调整):

var results = new Array();
$("div[id*='MSO_ContentTable']:contains('[Rotator')").each(function(){
   var contents = $(this).text().split(' ');
   $.each(contents,function(){
      var search = /Rotator/;
      if (search.test(this){
      results.push(this);
      }
   });
});

请参阅此小提琴演示:http://jsfiddle.net/nnNcz/

答案 1 :(得分:1)

$("div[id*='MSO_ContentTable']:contains('[Rotator')").each(function() {
    var elements = [];

    $(this).contents().filter(function () {
        return this.nodeType === 3
    }).each(function () {
        elements = elements.concat(this.nodeValue.match(/\[Rotator(.+?)\]/g));
    });

    console.log(elements);
});​

演示:http://jsfiddle.net/z8pPr/1/