字符串转换为数组时丢失前导 0

发布于 2024-12-08 17:33:34 字数 754 浏览 0 评论 0原文

我有一个 textInput 控件,它将 .txt 值发送到数组集合。数组集合是美国邮政编码的集合,因此我使用正则表达式来确保只从 textInput 中获取数字。

private function addSingle(stringLoader:ArrayCollection):ArrayCollection {
  arrayString += (txtSingle.text) + '';
  var re:RegExp = /\D/;
  var newArray:Array = arrayString.split(re);

美国邮政编码从 00501 开始。在调试器之后,提交 zip 后,变量“arrayString”为 00501。但是一旦为“newArray”分配了一个值,它就会删除前两个 0,留下 501。是这样吗?我的正则表达式做了一些我没有预料到的事情?难道是数组改变了值?我用 javascript 编写了一个正则表达式测试。

<script type="text/javascript">
  var str="00501"; 
  var patt1=/\D/;
  document.write(str.match(patt1));
</script>

我得到 null,这让我相信我使用的正则表达式没问题。在 split 方法的帮助文档中,我没有看到任何关于前导 0 存在问题的参考。

**我已经从代码中完全删除了正则表达式,但同样的问题仍然发生。这意味着问题不是来自正则表达式。

I have a textInput control that sends .txt value to an array collection. The array collection is a collection of US zip codes so I use a regular expression to ensure I only get digits from the textInput.

private function addSingle(stringLoader:ArrayCollection):ArrayCollection {
  arrayString += (txtSingle.text) + '';
  var re:RegExp = /\D/;
  var newArray:Array = arrayString.split(re);

The US zip codes start at 00501. Following the debugger, after the zip is submitted, the variable 'arrayString' is 00501. But once 'newArray' is assigned a vaule, it removes the first two 0s and leaves me with 501. Is this my regular expression doing something I'm not expecting? Could it be the array changing the value? I wrote a regexp test in javascript.

<script type="text/javascript">
  var str="00501"; 
  var patt1=/\D/;
  document.write(str.match(patt1));
</script>

and i get null, which leads me to believe the regexp Im using is fine. In the help docs on the split method, I dont see any reference to leading 0s being a problem.

**I have removed the regular expression from my code completely and the same problem is still happening. Which means it is not the regular expression where the problem is coming from.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

豆芽 2024-12-15 17:33:34

运行这个简化的案例:

var arrayString:String = '00501';
var re:RegExp = /\D/;
var newArray:Array = arrayString.split(re);
trace(newArray);

按预期生成 '00501'。您发布的代码中没有任何内容会删除前导零。您可能想进一步挖掘。

这听起来很像数字强制转换:Number('00501') 产生 501。仔细阅读隐式转换文档并检查是否有任何弹出窗口在你的代码中。

Running this simplified case:

var arrayString:String = '00501';
var re:RegExp = /\D/;
var newArray:Array = arrayString.split(re);
trace(newArray);

Yields '00501' as expected. There's nothing in the code you've posted that would strip leading zeros. You may want to dig around a bit more.

This smells suspiciously like Number coercion: Number('00501') yields 501. Read through the docs for implicit conversions and check if any pop up in your code.

Oo萌小芽oO 2024-12-15 17:33:34

这又如何呢?

/^\d+$/

您还可以指定 5 个数字,如下所示:

/^\d{5}$/

What about this ?

/^\d+$/

You can also specify exactly 5 numbers like this :

/^\d{5}$/
来日方长 2024-12-15 17:33:34

我建议只获取邮政编码,而不是分割非数字(特别是如果“arrayString”可能有多个邮政编码):

var newArray:Array = [];
var pattern:RegExp = /(\d+)/g;
var zipObject:Object;

while ((zipObject = pattern.exec(arrayString)) != null)
{
    newArray.push(zipObject[1]);
}

for (var i:int = 0; i < newArray.length; i++)
{
    trace("zip code " + i + " is: " + newArray[i]);
}

I recommend just getting the zip codes instead of splitting on non-digits (especially if 'arrayString' might have multiple zip codes):

var newArray:Array = [];
var pattern:RegExp = /(\d+)/g;
var zipObject:Object;

while ((zipObject = pattern.exec(arrayString)) != null)
{
    newArray.push(zipObject[1]);
}

for (var i:int = 0; i < newArray.length; i++)
{
    trace("zip code " + i + " is: " + newArray[i]);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文