简体   繁体   English

Javascript名称字段屏蔽(使用正则表达式)

[英]Javascript name field masking (Using regular expression)

i need to mask a name and i want it in Script. 我需要掩盖名称,并且希望在脚本中使用它。 i have a list format data. 我有一个列表格式数据。 Following is my jsp which put data in a variable. 以下是我的jsp,它将数据放入变量中。

<c:forEach value="${test}" var="v"/>
<c:set var = "nametest" value="${v.name}"

i only need names to be shown on the screen. 我只需要在屏幕上显示名称。 so i made a variable only contains names. 所以我做了一个仅包含名称的变量。 The problem is my script function doesn't look like contain my data correctly. 问题是我的脚本函数看起来不正确包含我的数据。 Following is my script. 以下是我的脚本。

function maskingName(nametest){
  var a = "${nametest}"
  if (a === undefined || a ===''){
  return '';
}
var pattern = /.$/;

return a.replace(pattern,"*");
}

after running it, i only get the last name (there are 10 names and it shows the only the last one)without masking. 运行它后,我只会得到姓氏(有10个名字,并且它仅显示姓氏)而没有遮罩。

My question is 1. how can i use List format in my script function? 我的问题是1.我如何在脚本功能中使用列表格式? 2. why does the regular expression not working? 2.为什么正则表达式不起作用?

Thank you! 谢谢!

I have written a small java code that might help you understand better 我写了一个小的Java代码,可以帮助您更好地理解

static String maskingName(String nametest) {

    if (nametest == null || nametest == "") {
        return "";
    }
    String pStr = ".$"  // ".*$";
    Pattern pattern = Pattern.compile(pStr);
    return pattern.matcher(nametest).replaceAll("***");
}

For input '12345' or '345345', the output will be '1234***' or '34534***' 对于输入'12345'或'345345',输出将为'1234 ***'或'34534 ***'

Replacing pStr=".*$" will give output ****** 替换pStr=".*$"将给出输出******

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM