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:
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!