使用Zapier的代码格式化邮政编码。

问题描述 投票:-1回答:1

我想知道是否有人成功地为Code by Zapier编写了任何成功格式化邮政编码的JavaScript。我觉得这应该是一个快速和简单的3行代码,但我可能尝试,我一直没有能够得到任何工作。它甚至没有toUpperCase(),所以我开始觉得这比我最初想象的要麻烦得多,而且如果我说实话,我没有能力想出一个复杂的解决方案来解决这个问题。

也许有人能帮我解决这个问题。

如果你对邮政编码不熟悉,基本上我收到的客户提交的数据是这样的。

A2a2a2
a2a2a2
a2a 2a2
A2A2A2
a2A2A2

我需要把它格式化成这样。

A2A 2A2

邮政代码永远是字母,数字,字母,数字,字母,数字, 我喜欢的格式是中间有空格。帮帮我,我给你个数字金星<3。

javascript zapier
1个回答
0
投票

在javascript中,你可以做这样的事情。

// Supposing you have the 6-character postal code stored in a variable named 'code'
// Convert the code into uppercase
code = code.toUpperCase();
let formattedCode = "";

// You can check if the space is the fourth character in the string, and do nothing else in that case
// If the space is not present, you can make a new string by concatenating the first three characters, a whitespace and the other three characters

if (code.charAt(3) != " ") {
    formattedCode = code.substring(0, 3).toUpperCase() + " " + code.substring(3, 6).toUpperCase();
}

我希望这能解决你的问题

© www.soinside.com 2019 - 2024. All rights reserved.