Home > Web Front-end > Front-end Q&A > How to replace asterisk in string in javascript

How to replace asterisk in string in javascript

藏色散人
Release: 2023-01-07 11:41:06
Original
4766 people have browsed it

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'
Copy after login

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

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

方法2(正则表达式):

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

测试结果: 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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template