Home  >  Article  >  Web Front-end  >  How to replace asterisk in string in javascript

How to replace asterisk in string in javascript

藏色散人
藏色散人Original
2021-06-27 10:54:024685browse

javascript实现字符串替换星号的方法:1、通过“phone.substr(0,3)+'****'+phone.substr(7);”方法实现替换;2、通过正则表达式实现字符串替换星号。

How to replace asterisk in string in javascript

本文操作环境:windows7系统、javascript1.8.5版、Dell G3电脑。

javascript怎么实现字符串替换星号?

JavaScript - 手机号码中间用星号代替

如手机号码18854112211,中间用’*'代替,显示188****2211

let phone='18854112211'

方法1(字符串的截取):

var showPhone = phone.substr(0,3)+'****'+phone.substr(7);

方法2(正则表达式):

var showPhone =  phone.replace(/^(\d{3})\d{4}(\d+)/,"$1****$2")

测试结果: 188****2211

【推荐学习:javascript高级教程

The above is the detailed content of How to replace asterisk in string in javascript. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn