简体   繁体   English

使用正则表达式替换除最后四位之外的所有数字。

[英]Replace all digits except the last four using regex.

I am using regex in javascript and want to replace all the digits in a number except the last four with '#' . 我在javascript中使用正则表达式,并希望用'#'替换除了最后四个之外的数字中的所有数字。 I have figured out how to replace all the digits with '#' , what do I do to not replace the last four? 我已经想出如何用'#'替换所有数字,我怎么做才能不替换最后四个? Here is my code so far. 到目前为止,这是我的代码。 return cc.replace(/[0-9]/g, "#")

use this pattern 使用这种模式

\d(?=\d{4})

and replace with # 并用#替换
Demo 演示

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

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