


Detailed explanation of the replace method in javascript regular expressions_javascript skills
In the previous article, I have introduced the four basic methods of regularization, and I also mentioned the replace method at that time
Let’s review the use of the replace method:
First define a regular object: var re=/write the matching conditions in the middle/;
replace(): Regularly match strings. If the match is successful, replace the successfully matched string with a new string
Syntax: string.replace(re, new string);
For example: It is often encountered on the Internet that uncivilized words will be replaced by *. Let’s try it:
<!DOCTYPE> <html> <head> <meta charset='utf-8'> <title></title> </head> <script type="text/javascript"> window.onload=function(){ var oTxtarea=document.getElementsByTagName('textarea'); var oInpt=document.getElementById('bt'); var re=/你妹|fuck|你大爷|萌萌/g; oTxtarea[0].value='我要看你妹,不行,你大爷,我要萌萌你妹,不行,你想想的太多了'; oInpt.onclick=function(){ oTxtarea[1].value=oTxtarea[0].value.replace(re,'*'); }; }; </script> <body> <textarea rows='7' cols='20'> </textarea><br /> <input id='bt' type='button' value='转化不文明的语言'><br /> <textarea rows='7' cols='20'> </textarea><br /> </body> </html>
Of course, we are not satisfied with the above conversion effect. What I want to achieve is to display several * signs after converting a few words
At this time we need to analyze. In fact, parameter 2 in replace(parameter 1, parameter 2) can be a callback function. Let’s transform the program, replace the second parameter with a callback function, and give This callback function passes a parameter
<!DOCTYPE> <html> <head> <meta charset='utf-8'> <title></title> </head> <script type="text/javascript"> window.onload=function(){ var oTxtarea=document.getElementsByTagName('textarea'); var oInpt=document.getElementById('bt'); var re=/你妹|fuck|你大爷|萌萌/g; oTxtarea[0].value='我要看你妹,不行,你大爷,我要萌萌你妹,不行,你想想的太多了'; oInpt.onclick=function(){ oTxtarea[1].value=oTxtarea[0].value.replace(re,function(obj){ alert(obj); /*alert(obj.length);*/ }); }; }; </script> <body> <textarea rows='7' cols='20'> </textarea><br /> <input id='bt' type='button' value='转化不文明的语言'><br /> <textarea rows='7' cols='20'> </textarea><br /> </body> </html>
It can be seen that the above result is very strange. The second parameter is the callback function, but when the parameters in the callback function are displayed, they are all successfully matched strings
Then we can process each result in this parameter, and a few words will generate several * signs
<!DOCTYPE> <html> <head> <meta charset='utf-8'> <title></title> </head> <script type="text/javascript"> window.onload=function(){ var oTxtarea=document.getElementsByTagName('textarea'); var oInpt=document.getElementById('bt'); var re=/你妹|fuck|你大爷|萌萌/g; oTxtarea[0].value='我要看你妹,不行,你大爷,我要萌萌你妹,不行,你想想的太多了'; oInpt.onclick=function(){ oTxtarea[1].value=oTxtarea[0].value.replace(re,function(obj){ var a=''; for (var i = 0; i < obj.length; i++) { a+='*'; } return a; }); }; }; </script> <body> <textarea rows='7' cols='20'> </textarea><br /> <input id='bt' type='button' value='转化不文明的语言'><br /> <textarea rows='7' cols='20'> </textarea><br /> </body> </html>
Through the above example, have you deepened your understanding of the replace method? . . . .
The above is the entire content of this article, I hope you all like it.

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Article discusses creating, publishing, and maintaining JavaScript libraries, focusing on planning, development, testing, documentation, and promotion strategies.

The article discusses strategies for optimizing JavaScript performance in browsers, focusing on reducing execution time and minimizing impact on page load speed.

Frequently Asked Questions and Solutions for Front-end Thermal Paper Ticket Printing In Front-end Development, Ticket Printing is a common requirement. However, many developers are implementing...

The article discusses effective JavaScript debugging using browser developer tools, focusing on setting breakpoints, using the console, and analyzing performance.

There is no absolute salary for Python and JavaScript developers, depending on skills and industry needs. 1. Python may be paid more in data science and machine learning. 2. JavaScript has great demand in front-end and full-stack development, and its salary is also considerable. 3. Influencing factors include experience, geographical location, company size and specific skills.

The article explains how to use source maps to debug minified JavaScript by mapping it back to the original code. It discusses enabling source maps, setting breakpoints, and using tools like Chrome DevTools and Webpack.

This tutorial will explain how to create pie, ring, and bubble charts using Chart.js. Previously, we have learned four chart types of Chart.js: line chart and bar chart (tutorial 2), as well as radar chart and polar region chart (tutorial 3). Create pie and ring charts Pie charts and ring charts are ideal for showing the proportions of a whole that is divided into different parts. For example, a pie chart can be used to show the percentage of male lions, female lions and young lions in a safari, or the percentage of votes that different candidates receive in the election. Pie charts are only suitable for comparing single parameters or datasets. It should be noted that the pie chart cannot draw entities with zero value because the angle of the fan in the pie chart depends on the numerical size of the data point. This means any entity with zero proportion

Once you have mastered the entry-level TypeScript tutorial, you should be able to write your own code in an IDE that supports TypeScript and compile it into JavaScript. This tutorial will dive into various data types in TypeScript. JavaScript has seven data types: Null, Undefined, Boolean, Number, String, Symbol (introduced by ES6) and Object. TypeScript defines more types on this basis, and this tutorial will cover all of them in detail. Null data type Like JavaScript, null in TypeScript
