2014-03-19 2 views
1

나는 최대 30 자 (완전한 문자)를 표시하는 데모를 만들고있다. 괜찮아 질 것이다.하지만 내 주어진 문자열에 br 태그가있을 때 내 문제가 실패한다. 우리 모두가 그 브레이크 라인을 알고 있으므로 .so 내가 원한다. 언제 내가 언제 브래그 태그를 발견했는지 깨기. 제가 문자열이 때jquery를 사용하여 문자열에서 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) 
+0

당신은 내가 넣어이 벤 나델 벤 나델이 Sachin 라 메쉬 Tendulkar는가에 다양한 방법을 검토되어 다시 –

답변

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/

+0

을 사용하시기 바랍니다 수 있습니다 전 Ramesh Tendulkar는 전 인도 크리켓 선수가 가장 많습니다. –

+0

을 당신을 말할 것이다 ..sorry 당신이 질문을 이해하지 생각 바이올린 –

+0

Ben Nadel이 맞습니다. Ben Nadel이 필요하지 않습니다. 두 번째 라인은 다양한 리뷰 여야합니다. –

1

당신은 <br />의 첫 번째 위치의 인덱스를 반환하는 yourString.indexOf('<br />')을 사용할 수 있습니다 -1 찾을 수 없습니다 <br />가없는 경우.

0

다른 생각 (나는 무슨 일이 일어나고 있는지 볼 수있는 일부 서식을 추가). http://jsfiddle.net/yzaaJ/13/

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