Home  >  Article  >  Web Front-end  >  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

高洛峰
高洛峰Original
2017-01-14 09:34:121686browse

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>

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!


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