当前位置:网站首页>JS filter / replace sensitive characters

JS filter / replace sensitive characters

2022-07-23 16:05:00 Smiling Chen sir

Text box for input content

   <input type="text" id="ip1"> 

Replace button

   <button id="bt1"> Replace </button>

 Insert picture description here

Minor case requirements :

// Replace unhealthy ***
// When you click the replace button , Get the user's input , If the access content contains violence , Replace it with ***
// Click event onclick

  var bt1=document.getElementById("bt1")
  bt1.onclick=function(){
    
  var ip1=document.getElementById("ip1")
  var content=ip1.value

// take content Medium sensitive characters Replace with ***
// Replace the string specified in the string

Console console.log(content) Output

 Insert picture description here

     content=content.replace(" unhealthy ","***") 
    // console.log(content)
    ip1.value=content
   }

 Insert picture description here

That ends here , But it's not completely over .

Thinking questions :
 Insert picture description here

As shown in the figure above : Suppose you enter two... In the text box / Multiple sensitive characters ( unhealthy ), Clicking the replace button will only replace the first unhealthy one with ***, If there is a second / The result of multiple unhealthy clicks on the replace button is zsdcaasc***, In this way , The problem arises , How can we replace all specified strings ? Of course, this question is left for everyone to think .

原网站

版权声明
本文为[Smiling Chen sir]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/204/202207231145273886.html