Internet Explorer返回错误的字符串长度 [英] Internet Explorer Returning Wrong Length of String

查看:146
本文介绍了Internet Explorer返回错误的字符串长度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了IE调用JavaScript的可能错误。 length 函数返回一个值,如果/当字符串派生自 .toLocaleString()

I've ran into a possible bug with IE where calling the JavaScript .length function returns a value that is off by 1 if/when the string was derived from .toLocaleString().

var d = new Date();
var locale = navigator.language;
var month = d.toLocaleString(locale, { month: "long"});
// month.length will return the length of the month string +1 
//(eg: if month = "March", month.length will return 6.)

有趣的是,上面的代码示例将返回true(在IE中)以下内容:(month [0]应该是M)

Interestingly, the code example of above will return true (in IE) for the following: (month[0] should be "M")

month[0] == "";
month[1] == "M";
month[2] == "a";
month[3] == "r";
month[4] == "c";
month[5] == "h";

在我的特定情况下,这导致我需要的问题。 slice()月份。示例:如果月份是3月,那么IE将为 month.slice(0,3)而不是Mar返回Ma。

In my particular case, this is causing a problem where I need to .slice() the month. Example: If the month is March, then IE will return "Ma" for month.slice(0,3) instead of "Mar".

这是IE的已知错误吗?是否有针对此问题的修复和/或解决方法?

Is this a known bug with IE? Is there a fix and/or workaround for this problem?

运行此,并注意IE中 month.length 的错误。

我的环境:


操作系统:Win Server 2012 R2

OS: Win Server 2012 R2

IE版本:11.0.9600.18231(更新版本:11.0.29)

IE Version: 11.0.9600.18231 (Update Versions: 11.0.29)

区域设置:英语/美国


推荐答案

所以,我偶然发现了这篇文章 toLocaleDateString错误

So, I stumbled upon this post toLocaleDateString error in IE 11

它似乎是由toLocaleDateString函数在IE11中添加额外的LTR和RTL字符引起的。其中一条评论给出了正在使用的正则表达式替换功能。

It appears it is caused by the toLocaleDateString function added extra LTR and RTL characters in IE11. One of the comments gave a regex replace function that is working for me.

month.replace(/[^ -~]/g,'');

尝试在执行 .toLocaleDateString(),它应该工作。它对我有用。

Try adding that after you perform the .toLocaleDateString() and it should work. It worked for me.

我们鄙视IE的另一个原因。

Just another reason for us to despise IE.

这篇关于Internet Explorer返回错误的字符串长度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆