【问题标题】:Return Last 4 Digits of Phone Number in Velocity在 Velocity 中返回电话号码的最后 4 位数字
【发布时间】:2017-03-17 06:22:58
【问题描述】:

我需要返回电话号码字段的最后 4 位(格式不一致)以显示为 PIN。

为我的怪诞尝试道歉,但我可以在下面更改什么以将 $PIN 显示为电话字段的最后 4 位数字?

#set ($PIN = ${lead.IR_Main_Phone__c}.substring(0,-4))

【问题讨论】:

  • 感谢您的回复。 Velocity 既不是直接的 Java 也不是 JS(见这里:velocity.apache.org/engine/1.7/vtl-reference.html)。不幸的是 substring(X,X) 有效,但 substring(X,-X) 无效。
  • 您可以尝试我发布的替代方案,没有子字符串,只是纯正则表达式

标签: email velocity velocity.js


【解决方案1】:

您应该过滤 lead.IR_Main_Phone__c var 中的非数字字符,然后获取最后 4 位数字。

例子:

String phoneno = "+1-(800)-555-2468";
$phoneno.replaceAll("\D", ""); //Removes non-digit characters 
$PIN = $phoneno.substring(0,-4) //gives: 2468

另外,没有子字符串的另一种方式:

String phoneno = "+1-(800)-555-2468";
$phoneno.replaceAll("\D", ""); //Removes non-digit characters 
$phoneno.replaceAll("(?=\d{5})\d", ""); //removes all but last 4 digits 
$PIN = $phoneno //gives: 2468

【讨论】:

    【解决方案2】:

    velocity 不是一个动画库吗? 只需使用常规 javascript

    var str = "123-4 5 6 7";
    var numbersOnly = str.match(/\d+/g, str).join('');
    console.log(numbersOnly.length); // outputs 7
    console.log(numbersOnly.substr(numbersOnly.length-4)); //output "4567"
    

    编辑:现在使用正则表达式过滤掉非数字

    【讨论】:

    • OP 写道格式不一致,所以最后四个字符可能是“9 8 7 6 1 2”中的“1 2”。你能想出一种使用正则表达式从字符串中只提取数字并得到最后四个的方法吗?
    猜你喜欢
    • 1970-01-01
    • 2016-03-02
    • 1970-01-01
    • 2021-06-01
    • 2017-01-14
    • 1970-01-01
    • 1970-01-01
    • 2023-02-20
    • 1970-01-01
    相关资源
    最近更新 更多