[英]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
}
I used typeof
to make sure both them are of the type string and not Object我使用typeof
来确保它们都是字符串类型而不是对象
I used replace(/ä/g, 'ae')
to eliminate the Umlaute我使用replace(/ä/g, 'ae')
来消除元音
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.