如何使用jquery检测字符串中的br标记?

时间:2014-03-19 18:29:11

标签: javascript jquery

我正在制作一个演示,其中我显示最多30个字符(完整的字符)。它将运行正常。但是当我的给定字符串中有br标签时我的问题失败。我们都知道br断线。所以我想要在我找到br标签的地方打破。 当我有这个字符串时:

var str = "Ben Nadel reviews the various approaches to Sachin Ramesh Tendulkar is a former Indian cricketer widely acknowledged as the greatest batsman of the modern generation, popularly holds the title  among his fans Sachin Ramesh Tendulkar is a former Indian cricketer widely acknowledged as the greatest batsman of the modern generation, popularly holds the title  among his fansSachin Ramesh Tendulkar is a former Indian cricketer widely acknowledged as the greatest batsman of the modern generation, popularly holds the title  among his fans  substring";

输出

Ben Nadel reviews the various
approaches to Sachin Ramesh
Tendulkar is a former Indian
cricketer widely acknowledged
as the greatest batsman of the
modern generation, popularly
holds the title among his
fans Sachin Ramesh Tendulkar
is a former Indian cricketer
widely acknowledged as the
greatest batsman of the modern
generation, popularly holds
the title among his
fansSachin Ramesh Tendulkar is
a former Indian cricketer
widely acknowledged as the
greatest batsman of the modern
generation, popularly holds
the title among his fans
substring

没关系。 但是当我在我的字符串中添加br标签时,我的概念无法显示30个字符。

  br string
var str = "Ben Nadel <br /> reviews the various approaches to Sachin Ramesh Tendulkar is a former <br /> Indian cricketer widely acknowledged as the greatest batsman of the modern <br /> generation, popularly holds the title  among his fans Sachin Ramesh Tendulkar is a former Indian cricketer <br /> widely acknowledged as the greatest batsman of the modern generation, popularly holds the title  among his fansSachin Ramesh Tendulkar is a former Indian cricketer widely acknowledged as the greatest batsman of the modern generation, popularly holds the title  <br /> among his fans  substring";

Ben Nadel   // explain here user find br tag it fine to go next line 
reviews the  // why not it display 30 characters ?
various approaches to Sachin
Ramesh Tendulkar is a former
                             //why boac space display
Indian cricketer widely
acknowledged as the greatest
batsman of the modern 
generation, popularly holds
the title among his fans
Sachin Ramesh Tendulkar is a
former Indian cricketer 
widely acknowledged as the
greatest batsman of the modern
generation, popularly holds
the title among his
fansSachin Ramesh Tendulkar is
a former Indian cricketer
widely acknowledged as the
greatest batsman of the modern
generation, popularly holds
the title                    //why 30 character not display ?
among his
fans substring

我想要一些逻辑。如果字符串中有br标签它会出现在新行中并再次显示30个字符(整个单词)? 我们可以这样做吗? http://jsfiddle.net/yzaaJ/11/

for(var index = 0; index < words.length; index++)
{
    var currentWord = words[index];
    var currentLength = tenLengthString.length;    
    if(((currentLength + currentWord.length + ((currentLength > 0) ? 1: 0))) > 30)
    {        
        html+='<div>'+tenLengthString+'</div>';
        console.log(tenLengthString);        
        tenLengthString = currentWord;        
    } else {
        if(currentLength > 0)
            tenLengthString += " ";    
        tenLengthString += currentWord;
    }    
    if(index == words.length - 1){
      console.log(tenLengthString);
       html+='<div>'+tenLengthString+'</div>';

    }        
}
$("#test").html(html)

3 个答案:

答案 0 :(得分:1)

如果找不到yourString.indexOf('<br />'),您可以使用<br />返回<br />第一个位置的索引或-1。

答案 1 :(得分:1)

由于你想保留休息时间,你应该首先将字符串分开。然后使用每个块的现有代码。

function cleanBreak(str){
    return str
            .replace(/<br >/g, "<br>")
            .replace(/<br \/>/g, "<br>")
            .replace(/<br\/>/g, "<br>");
}

var str = "Ben Nadel <br /> reviews the various approaches to Sachin Ramesh Tendulkar is a former <br /> Indian cricketer widely acknowledged as the greatest batsman of the modern <br /> generation, popularly holds the title  among his fans Sachin Ramesh Tendulkar is a former Indian cricketer <br /> widely acknowledged as the greatest batsman of the modern generation, popularly holds the title  among his fansSachin Ramesh Tendulkar is a former Indian cricketer widely acknowledged as the greatest batsman of the modern generation, popularly holds the title  <br /> among his fans  substring";
var phrases = cleanBreak(str).split("<br>");
var html=[];
for(var pIndex = 0; pIndex < phrases.length; pIndex++){
    html.push("<div class='phrase'>");
    var words = phrases[pIndex].split(" ");
    var tenLengthString = "";
    for(var index = 0; index < words.length; index++)
    {
        var currentWord = words[index];
        var currentLength = tenLengthString.length;    
        if(((currentLength + currentWord.length + ((currentLength > 0) ? 1: 0))) > 30)
        {        
            html.push('<div>'+tenLengthString+'</div>');
            console.log(tenLengthString);        
            tenLengthString = currentWord;        
        } else {
            if(currentLength > 0)
                tenLengthString += " ";    
            tenLengthString += currentWord;
        }    
        if(index == words.length - 1){
          console.log(tenLengthString);
           html.push('<div>'+tenLengthString+'</div>');

        }        
    }
    html.push("</div>");
}
$("#test").html(html.join(''));

http://jsfiddle.net/9TZhE/ (我添加了一些格式以查看正在进行的操作)

答案 2 :(得分:0)

不同的想法。 http://jsfiddle.net/yzaaJ/13/

html = html.replace(/<br >/g, "").replace(/<br \/>/g, "")      .replace(/<br\/>/g, "");
$("#test").html(html)