【发布时间】:2019-09-25 16:22:47
【问题描述】:
我有这个脚本:
let stdin = '';
process.stdin
.setEncoding('utf8')
.resume()
.on('data', d => {
stdin+= d;
});
const regexs = [
[/(?=.*[^a-zA-Z])[0-9a-zA-Z]{7,300}/g, function replacer(match, p1, p2, p3, offset, string) {
return match.slice(0,2) + 'xxxxx' + match.slice(6);
}]
];
process.stdin.once('end', end => {
for(let r of regexs){
stdin = stdin.replace(r[0], r[1]);
}
console.log(stdin);
});
我就是这样使用它的:
echo "ageoageag ageagoie ag77eage" | node sanitize-stdin.js
我明白了:
agxxxxxeag agxxxxxie agxxxxxge
但我真的只想替换长度为 6-300 的字符串(如果其中包含数字)。所以我正在寻找的输出是:
ageoageag ageagoie agxxxxxge
任何人都知道如何仅在字符串中至少有一个数字的情况下替换该字符串?
【问题讨论】:
标签: node.js regex sanitization