简体   繁体   English

JavaScript:字符串相等但比较返回false

[英]JavaScript: Strings are equal but comparing returns false

I'm currently developing theme for Textual IRC and I want to compare the "Topic is ..." messages to the topic displayed in the channels topic bar, to delete them if the are the same.我目前正在为 Textual IRC 开发主题,我想将“主题是 ...”消息与频道主题栏中显示的主题进行比较,如果相同,则删除它们。

The topic that causes problems has both Umlaute and a URI in it and looks like the following:导致问题的主题中包含 Umlaute 和 URI,如下所示:

++ Frische Austern ++ Nächste Sitzung: https:/some/uri/that/can/contain/umlaute" ++

When I print both the old topic and the new topic, they look exactly the same, down to the trailing and leading whitespaces (I used trim() to eliminate them).当我打印旧主题和新主题时,它们看起来完全相同,直到尾随和前导空格(我使用了trim()来消除它们)。

The comparison is done with比较是用

if(oldTopic === newTopic){
    // do stuff
}

What I've tried so far到目前为止我尝试过的

Typecheck类型检查

I used typeof to make sure both them are of the type string and not Object我使用typeof来确保它们都是字符串类型而不是对象

Umlaut elimination元音消除

I used replace(/ä/g, 'ae') to eliminate the Umlaute我使用replace(/ä/g, 'ae')来消除元音

URL Elimination网址消除

I used replace(/\\//g, '_') to get rid of the forward slashes I used escape() to escape non unicode characters我用replace(/\\//g, '_')去掉了我用escape()转义非 unicode 字符的正斜杠

Unfortunately none of it worked.不幸的是,没有一个奏效。 Again if I use console.log to show the two strings, they are exactly the same.同样,如果我使用console.log来显示两个字符串,它们是完全相同的。 I was expecting some Unicode stuff, that you can represent ä in different ways, but replacing it didn't work either.我期待一些 Unicode 的东西,你可以用不同的方式表示ä ,但替换它也不起作用。

I've tried but I reached my limit of my JavaScript knowledge.我试过了,但我的 JavaScript 知识达到了极限。 I have really no idea why it's not working.我真的不知道为什么它不起作用。 The code has worked on some other topics, that did neither involve any Umlaute nor an URL.该代码适用于其他一些主题,既不涉及任何元音变音也不涉及 URL。

If any of you happens to know an answer I'd be very thankful.如果你们中的任何人碰巧知道答案,我将不胜感激。

Kind regards and thanks in advance!提前致以亲切的问候和感谢!

So in the end the whitespace right before the https part was a different type of whitespace, than all the others.所以最后, https部分之前的空白是一种不同类型的空白,而不是所有其他空白。

It was not a tab and I tried different regular expression symbols to get it ( \\f , \\r and stuff) but it didn't work out.它不是选项卡,我尝试了不同的正则表达式符号来获取它( \\f\\r和其他东西),但没有成功。

What worked in the end was using replace(/\\s/g, '') .最后有效的是使用replace(/\\s/g, '') \\s also covers tabs, but I assume, that there probably won't be a topic change, that has no other change, than changing a whitespace to a tab. \\s还包括选项卡,但我认为,除了将空格更改为选项卡之外,可能不会有主题更改,没有其他更改。

Just keep in mind, that if tabs and whitespaces have to make a difference in your case, this solution won't work.请记住,如果制表符和空格必须对您的情况产生影响,则此解决方案将不起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在JQuery中比较两个相同的字符串将返回false - Comparing two identical strings in JQuery returns false 两个相等字符串的比较返回false - Comparision of two equal strings returns false 比较2个看似相等的字符串会返回false,因为一个以char代码8291结尾的字符串。如何清理字符串? - Comparing 2 seemingly equal strings returns false because one ends with char code 8291. How can I sanitize the string? JavaScript-比较两个相同的字符串但它们不相等 - JavaScript - comparing two same strings but they are not equal Javascript:比较标准函数总是返回* false * - Javascript: Comparing standard functions always returns *false* 比较两个相等节点的childNodes返回false,比较它们的innerHTML返回true,为什么? - Comparing two equal nodes' childNodes returns false, comparing their innerHTML returns true, why? Ember.computed.equal在相同的字符串上返回false - Ember.computed.equal returns false on identical strings 用javascript比较数组中的两个字符串以查看它们是否相等 - javascript-comparing two strings in an array to see if they are equal JavaScript将NUMBER.MIN_VALUE与0.0进行比较会返回false - JavaScript comparing NUMBER.MIN_VALUE to 0.0 returns false 为什么在JavaScript中将value与undefined的比较返回false? - Why does comparing value with undefined returns false in JavaScript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM