2

I'm trying to do something fancy with a blogger blog, and when I'm looking at a particular blog, I'd like to be able to know which one it is.

So, although there might be a better way of doing it, I've got this bit of code set up:

//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);
    }
  }
}

Where titles[] is an array of titles read in from the RSS feed (the idea being that, if I get the index for the title, I can apply that to another array I've read from said feed).

Thing is, while the first alert produces two strings that appear identical, they aren't picked up by the if... statement. I've added lines to set the two variables to the same string, and that picks them up. The only solution I can even think of is that I'm missing a hidden character or something in one of the strings, but I think I have all of them covered! Anyone got any ideas?

4

4 に答える 4

3

と を試して\nください\r.trim()余分なスペースを削除するためにa を実行することもできます。

編集: Jonas が指摘したように、JavaScript にはネイティブの .trim() 関数がないため、このリンクで説明されているように独自の関数を作成できます。

于 2010-01-18T15:57:58.843 に答える
2

すでにプレーンテキストであるタイトルからタグのような構造を削除しようとしているのはなぜですか? (From innerText; は IE でのみ機能します。他のブラウザーではtextContent代わりに standard プロパティが必要になります。) RSS タイトルは HTML-in-XML で二重エンコードされません (ただし、長い説明は可能です)。

\nとにかく、 andを削除しようとして\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 版のメソッドを使用しています:mapおよびindexOfonArrayおよびtrimon String. ただし、言語をサポートしていないブラウザーの場合は、自分で言語に追加できます。

// 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+$/, '');
    };
}
于 2010-01-18T16:23:31.920 に答える
0

いくつかのデバッグのヒント: 改行とキャリッジ リターンの置換で、バックスラッシュの代わりにスラッシュを使用しています。\n & \r である必要があります。警告するときは、各タイトルの前後に文字を入れてみて、文字列の境界を確認できるようにします。不足している文字が見つからない場合は、for ループを使用し、各文字列をステップ実行して、string.charAt(i) を使用してすべての文字を確認します。文字列の比較では、== の代わりに === を使用する必要があります。

于 2010-01-18T16:22:49.867 に答える
0

あなたは試すことができます

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

ストリングスをトリミングtoUpperCase()することで、どこかへつながるかもしれません。


于 2010-01-18T16:01:01.213 に答える