2010-01-18 3 views
2

블로거 블로그로 멋진 것을하려고하고 있는데, 특정 블로그를보고있을 때 어떤 블로그인지 알고 싶습니다. . 제목 []하는 제목의 배열 인문자열 비교 (아마도 숨겨진 문자 문제 일까?)

//Get title of current blog 
currentTitle = document.getElementById("post-title").innerText; 
currentTitle = currentTitle.replace(/<\/?[^>]+(>|$)/g, ""); 
currentTitle = currentTitle.replace("/n",""); 
currentTitle = currentTitle.replace("/r",""); 
//Run through titles until you find a match 
for (count = 0; count <= titles.length; count++) 
{ 
    //alert(currentTitle); 
    //alert(titles[count]); 
    if (titles[count] != null) 
    { 
    checkTitle = titles[count]; 
checkTitle = checkTitle.replace(/<\/?[^>]+(>|$)/g, ""); 
    checkTitle = checkTitle.replace("/n",""); 
    checkTitle = checkTitle.replace("/r",""); 
alert(checkTitle.toString()+" + "+currentTitle.toString()); 
    if (checkTitle.toString() == currentTitle.toString()) 
    { 
     alert(count); 
    } 
    } 
} 

는 RSS 피드에서 읽기 : 그 일을 더 나은 방법이있을 수도 있지만

그래서, 나는 코드의이 비트가 설정있어 (아이디어는, 내가 제목에 대한 색인을 얻으면, 나는 그 피드에서 읽은 또 다른 배열에 적용 할 수있다).

첫 번째 경고는 동일한 것으로 보이는 두 개의 문자열을 생성하지만, if ... 문은 해당 문자열을 선택하지 않습니다. 두 변수를 같은 문자열로 설정하는 줄을 추가했습니다. 내가 생각할 수있는 유일한 해결책은 문자열 중 하나에 숨겨진 캐릭터 나 무언가가 빠져 있다는 것입니다. 그러나 나는 그것들 모두를 다 커버했다고 생각합니다! 누구든지 아이디어가있어?

+1

공백 또는 공백을 확인 했습니까? – Samuel

답변

3

시도 \n\r. 여분의 공백을 제거하기 위해 .trim()을 할 수도 있습니다.

EDIT : Jonas는 JavaScript에 고유 한 .trim() 함수가 없으므로 this link이 설명하는대로 사용자 고유의 .trim() 함수를 만들 수 있다고 지적했습니다.

+1

JavaScript에 트림 기능이 내장되어 있지 않습니다. 그냥 '문자열을 추가하십시오.prototype.trim = function() {return this.replace (/^\ s + | \ s + $/g, ""); }; ' –

+0

아, 내 실수, 너무 많은 언어가 요즘 스와핑 :) –

0

당신은 어딘가에 이어질 수도

alert(checkTitle.toString().length+" + "+currentTitle.toString().length); 

toUpperCase()trimming 문자열을 시도 할 수 있습니다.


0

몇 가지 디버깅 힌트 : 개행과 캐리지 리턴 대체에 슬래시 대신 슬래시가 있어야합니다. 그들은 \ n & \ r이어야합니다. 경고 할 때 문자열의 경계를 볼 수 있도록 각 제목 앞뒤에 문자를 넣으십시오. 그 문자가 누락 된 문자를 표시하지 않으면 for 루프를 사용하고 각 문자열을 단계별로 실행하여 string.charAt (i)를 사용하여 모든 문자를 봅니다. 문자열 비교를 위해서는 == 대신 ===를 사용해야합니다.

2

왜 이미 일반 텍스트 인 제목에서 태그와 같은 구조를 제거하려고합니까 (정규식을 사용하지 않는지, 신뢰할 수는 없지만 그렇지 않습니다)? (innerText에서 : IE에서만 작동하며, 다른 브라우저에서는 표준 textContent 속성이 필요합니다.) RSS 제목은 XML로 된 이중 인코딩 HTML이 아닙니다 (긴 설명도 가능하지만).

어쨌든 \n\r을 제거하려고했지만 잘못된 종류의 슬래시가 있습니다. 여전히 전후방에 공백이나 탭이있을 수 있습니다. 숀 말했듯이, 당신은 더 나은 비교하기 전에 모든 공백 문자를 잘라 것, 예를 들면 :

// assuming each item in `titles` has already been trimmed too 
var el= document.getElementById("post-title"); 
var title= 'textContent' in el? el.textContent : el.innerText; 
var count= titles.indexOf(title.trim()); 

그러나,이 좋은 간결 코드는 아직 모든 브라우저에서 사용할 수없는 몇 가지 인 ECMAScript 5 판의 방법을 사용 mapindexOf은 이고 trimString입니다. 그러나 지원하지 않는 브라우저의 경우 언어에 직접 추가 할 수 있습니다.

// Add some ECMA262-5 methods if not already supported natively 
// 
if (!('indexOf' in Array.prototype)) { 
    Array.prototype.indexOf= function(find, from) { 
     for (var i= from || 0, n= this.length; i<n; i++) 
      if (i in this && this[i]===find) 
       return i; 
     return -1; 
    }; 
} 
if (!('map' in Array.prototype)) { 
    Array.prototype.map= function(mapper, that) { 
     var other= new Array(this.length); 
     for (var i= 0, n= this.length; i<n; i++) 
      if (i in this) 
       other[i]= mapper.call(that, this[i], i, this); 
     return other; 
    }; 
} 
if (!('trim' in String.prototype)) { 
    String.prototype.trim= function() { 
     return this.replace(/^\s+/, '').replace(/\s+$/, ''); 
    }; 
}