简体   繁体   English

在JavaScript中同时使用String Replace with Regex时如何获取捕获组的长度?

[英]How to get length of captured groups when using String Replace with Regex in JavaScript concurrently?

I'm trying to achieve a simple Data Masking Function by replacing some parts of private data to asterisks(*) with javaScript string replace function. 我试图通过使用javaScript字符串替换功能将私有数据的某些部分替换为星号(*)来实现简单的数据屏蔽功能。

So what I've done is as following: 因此,我所做的如下:

  function privateDataMask(data, options) { var str = JSON.stringify(data); var i = 0, len = options.length; for (i; i < len; i++) { var opt = options[i]; var key = opt.key; var reg = opt.reg; var _reg = new RegExp('"' + key + '":"' + reg.source + '"', 'g'); //To get new reg which includes the key, like /"tel":"(\\d{3})(\\d*)(\\d{4})"/g str = str.replace(_reg, '"' + key + '":"$1*$3"'); //str = str.replace(_reg, '"' + key + '":"$1' + '*'.repeat($2.length) + '$3",'); //ReferenceError: $2 is not defined //str = str.replace(_reg, '"' + key + '":"$1' + '*'.repeat(RegExp.$2.length) + '$3",'); //https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/n } return JSON.parse(str); } var data = [{ name: "王梅", tel: "15812346584", id: "22040219820418866X" }, { name: "吴青峰", tel: "13921345678", id: "450328197006157868" }, { name: "王张玉成", tel: "17734685988", id: "620100198109198020" } ]; var options = [{ key: 'name', reg: /([\一-\龥A-Za-z0-9\\·\\.]?)([\一-\龥A-Za-z0-9\\·\\.]+)([\一-\龥A-Za-z0-9\\·\\.]+)/ }, { key: 'tel', reg: /(\\d{3})(\\d*)(\\d{4})/ }, { key: 'id', reg: /(\\d{6})(\\d*)(\\d{5}(\\d|X){1})/ } ] var maskedData = privateDataMask(data, options); console.log(maskedData); 

It works somehow as expected, but still, I want to make little improvements, which can transfer for example, name: "吴青峰" -> name: "吴*峰", name: "王张玉成" -> name: "王**成", tel: "15812346584" -> tel: "158****6584", etc. 它可以按预期的方式工作,但是我仍然想做一点改进,例如可以将名称:“吴青峰”->名称:“吴*峰”,名称:“王张玉成”->名称:“王**成”,电话:“ 15812346584”->电话:“ 158 **** 6584”,等等。

That is, adding multiple asterisks( ) to the parts replaced, and the number of asterisks( ) should equal to the length of captured group during String.replace(). 也就是说, 在替换的零件上添加多个asterisks( ), asterisks( )的数量应等于String.replace()期间捕获的组的长度。

As the commented lines indicate, I've tried things like '*'.repeat(RegExp.$2.length), the puzzle is RegExp.$n can only get value after the String.replace() function finishes, which leads to malposition. 如注释行所示,我已经尝试过类似'*'。repeat(RegExp。$ 2.length)之类的问题,难题是RegExp。$ n仅在String.replace()函数完成后才能获取值,这会导致位置错误。

Therefore, are there any methods to get the length of captured group concurrently during String.replace() function please ? 因此,请问有什么方法可以在String.replace()函数期间同时获取捕获组的长度?

You can pass a function as second parameter to .replace() and that function will calculate the amount of * will be added. 您可以将一个函数作为第二个参数传递给.replace() ,该函数将计算*的数量。

_str = _str.replace(_reg, (text, start, middle, end) => `"${key}":"${start + '*'.repeat(middle.length) + end}"`);

Demo: 演示:

  function privateDataMask(data, options) { var str = JSON.stringify(data); var i = 0; var len = options.length; var _str, _data; for (i; i < len; i++) { var opt = options[i]; var key = opt.key; var reg = opt.reg; var _reg = new RegExp('"' + key + '":"' + reg.source + '"', 'g'); //To get new reg which includes the key, like /"tel":"(\\d{3})(\\d*)(\\d{4})"/g //console.log(_reg); if (!_str) { _str = str; } _str = _str.replace(_reg, (text, start, middle, end) => `"${key}":"${start + '*'.repeat(middle.length) + end}"`); } _data = JSON.parse(_str); return _data; } var data = [{ name: "王梅", tel: "15812346584", id: "22040219820418866X" }, { name: "吴青峰", tel: "13921345678", id: "450328197006157868" }, { name: "王张玉成", tel: "17734685988", id: "620100198109198020" } ]; var options = [{ key: 'name', reg: /([\一-\龥A-Za-z0-9\\·\\.]?)([\一-\龥A-Za-z0-9\\·\\.]+)([\一-\龥A-Za-z0-9\\·\\.]+)/ }, { key: 'tel', reg: /(\\d{3})(\\d*)(\\d{4})/ }, { key: 'id', reg: /(\\d{6})(\\d*)(\\d{5}(\\d|X){1})/ } ] var maskedData = privateDataMask(data, options); console.log(maskedData); 

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

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