在Javascript中拆分字符串的奇怪行为 [英] Strange behaviour with spliting a string in Javascript

查看:117
本文介绍了在Javascript中拆分字符串的奇怪行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试做一些相对简单的事情。我有这种格式的日期dd / MM / yyyy例如:

I am trying to do something relatively simple. I have a date in this format dd/MM/yyyy eg:

var newDate = "‎11‎/‎06‎/‎2015";

我想将其转换为日期。

此代码仅适用于Chrome和Firefox:

This code only works in Chrome and Firefox:

new Date(newDate)

在IE11中,我得到 Nan

In IE11 I get Nan

所以我想这样做:

var parts = newDate.split("/");
var year = parts[2].trim();
var month = parts[1].trim();
var day = parts[0].trim();
var dt = new Date(Number(year), Number(month) - 1, Number(day));

哪个应该有效,但我遇到了一个非常奇怪的错误。

Which should work, but I have encountered a very strange bug.

如果您尝试此代码:

function myFunction() {
  var newDate = "‎11‎/‎06‎/‎2015";
  var parts = newDate.split('/');
  var year = parts[2].trim();

  var a = year;
  var b = Number(year);
  var c = parseInt(year, 10);
  var d = parts;
  var n = a + "<br>" + b + "<br>" + c + "<br>" + d;
  document.getElementById("demo").innerHTML = n;
}

<p>Click the button to see the parse error.</p>

<button onclick="myFunction()">Try it</button>

<p id="demo"></p>

然后在IE中它添加了一个神秘的角色,它打印出 2015年并在chrome中打印出?2015

Then in IE it adds a mystery character and it prints out ý2015 and in chrome it prints out ?2015.

事实上IE中部件的价值是:ý11ý,ý06ý,ý2015
在Chrome中:?11?,?06 ?,?2015

In fact the value of parts in IE is : ý11ý,ý06ý,ý2015 In Chrome: ?11?,?06?,?2015

我无法理解这些神秘人物的来源!我的原始字符串只是2015年11月06日

I can't understand where these mystery characters come from! My original string is just "‎11‎/‎06‎/‎2015"

似乎没有做一些简单的事情的方法,比如从一个简单的字符串中解析一个整数。

There seems to be no way for be to do something so simple, such as parsing an integer from a simple string.

小提琴不显示隐藏的字符,但我相信它们仍然存在,因为数字(2015)导致 NaN 你可以清楚地看到这里
有什么想法吗?

Fiddle doesn't show the hidden characters but I believe they are still there because Number("2015") results in NaN as you can see clearly here Any ideas?

字符串中确实隐藏了字符,经过调查我发现它们已被创建像这样:

There are indeed hidden characters in the string, and after investigation I found out that they are created like this:

var date = new Date();
var dateToSave = date.toLocaleDateString();

但仅限于IE。

In Chrome或Firefox的输出不包含 U + 200E 从左到右的标记,但在IE中它确实存在!

In Chrome or Firefox the output doesn't contain the U+200E left-to-right mark but in IE it does!

删除 toLocaleDateString()并将其替换为 kendo.toString(selectedValue,dd / MM / yyyy)解决了问题。

Removing toLocaleDateString() and replacing it with kendo.toString(selectedValue, "dd/MM/yyyy") fixed the problem.

为了记录我还尝试了moment.js和行:
moment(selectedValue).format( DD / MM / YYYY)但由于某些原因,IE11在结果字符串的最开头有一个隐藏的 U + 200E 字符。

For the record I also tried moment.js and the line: moment(selectedValue).format("DD/MM/YYYY") but for some reason in IE11 there was one hidden U+200E character at the very beginning of the result string.

推荐答案

我跑2015年11月6日.split(' ')。map(function(s){return s.charCodeAt(0)})(以获取Unicode值)在我的控制台中,找到了一些有趣的东西: [8206 ,49,49,8206,47,8206,48,54,8206,47,8206,50,48,49,53]

I ran "‎11‎/‎06‎/‎2015".split('').map(function(s){return s.charCodeAt(0)}) (to get the Unicode values) in my console, and found something interesting: [8206, 49, 49, 8206, 47, 8206, 48, 54, 8206, 47, 8206, 50, 48, 49, 53]

你有 U + 200E从左到右标记。我不知道它是怎么到达的。

You have a U+200E left-to-right mark in there. I don't know how it got there.

删除它,你会没事的。

在这里,您可以复制并粘贴我的字符串:2015年6月11日

Here, you can copy and paste the string from me: "11/06/2015".

这篇关于在Javascript中拆分字符串的奇怪行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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