String comparison (possibly a hidden character issue?)
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 o开发者_如何转开发f them covered! Anyone got any ideas?
Try \n
and \r
. You may want to also do a .trim()
to remove extra spaces.
EDIT: As Jonas pointed out, there's no native .trim() function in JavaScript so you can create your own as this link describes.
Why are you trying to remove tag-like structures (with regex, which is super-unreliable for this; don't) from titles that are already plain text? (From innerText
; only works in IE; other browsers would need the standard textContent
property instead.) RSS titles aren't double-encoded HTML-in-XML (though the long descriptions can be).
Anyhow, you're trying to remove \n
and \r
, but you've got the wrong kind of slashes. It's also entirely possible you'll still have leading and trailing spaces or tabs in there. As Shawn said, you'd better trim all these whitespace characters before comparison, eg.:
// 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());
However, this nice concise code uses a few ECMAScript Fifth Edition methods that aren't available in all browsers yet: map
and indexOf
on Array
and trim
on String
. You can, however, add them to the language yourself for browsers that don't support it:
// 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+$/, '');
};
}
You could try
alert(checkTitle.toString().length+" + "+currentTitle.toString().length);
and toUpperCase()
and trimming the strings might lead somewhere.
A couple debugging hints: you've got forward slashes instead of backslashes in your newline and carriage return replacements. They should be \n & \r. When you alert, try putting a character before and after each title so you can see the boundaries of the string. If that didn't reveal the character you're missing, use a for loop and step through each string to see all the characters using string.charAt(i). For string comparisons, you should be using === instead of ==.
精彩评论