Home > Web Front-end > JS Tutorial > JS uses regular expressions to filter multiple words and replace them with asterisks of the same length

JS uses regular expressions to filter multiple words and replace them with asterisks of the same length

高洛峰
Release: 2017-01-14 09:34:12
Original
1699 people have browsed it

The example in this article describes how JS uses regular expressions to filter multiple words and replace them with asterisks of the same length. Share it with everyone for your reference, the details are as follows:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>正则过滤</title>
</head>
<body>
<script type="text/javascript">
var text = "过滤敏感词及不文明用语";
var re = /敏感词|文明/gi;
var output = text.replace(re, function(sMatch){
 return sMatch.replace(/./g,"*");
});
document.write(output);
</script>
</body>
</html>
Copy after login

The running results are shown in the figure below:

JS uses regular expressions to filter multiple words and replace them with asterisks of the same length

I hope this article will be helpful Everyone JavaScript programming helps.

For more JS using regular expressions to filter multiple words and replace them with asterisks of the same length, please pay attention to the PHP Chinese website for related articles!


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