Regular replacement of replace
replace means replacement in English. There is also a replace function in programming, so how to use it? In this article, we will share with you the method of replace to replace the original characters with new characters, that is, in replace Regular replacement.
replace: Replace the original characters with new characters
1. Replace string replacement
var str = 'pku2016pku2017'; str = str.replace('pku', 'pkusoft'); console.log(str); // pkusoft2016pku2017
without using regular expressions Under, each execution can only replace one character. Each execution starts from 0. If there are duplicates, all cannot be replaced.
2. Regular replacement of replace
str = str.replace(/pku/g, 'pkusoft'); // 使用正则的全局匹配 console.log(str); // pkusoftsoft2016pkusoft2017
First, just like exec capture, capture all the regular expressions that match us, and then replace the captured content with the new content we need to replace.
/pku/gFollow this regular rule to capture all matching items in str, and then replace them all with 'pkusoft'
replace if the second parameter is a function
1. Anonymous function The number of times it is executed depends on how many times the regular expression can be captured in the string
2. Each time the anonymous function is executed, the arguments value is very similar to the content captured through exec
3. return The value is the content that needs to be replaced
str = str.replace(/pku/g, function () { console.log(arguments); // 第一次执行: ["pku", 0, "pku2016pku2017"] // 第一次执行: ["pku", 7, "pku2016pku2017"] // 返回的数组和执行exec返回的结果一致 return 'pkusoft'; }); console.log(str); // pkusoftsoft2016pkusoft2017
replace group capture
str = str.replace(/(\d+)/g, function () { // console.log(arguments); // 第一次执行: ["2016", "2016", 7, "pkusoft2016pkusoft2017"] // 第一次执行: ["2017", "2017", 18, "pkusoft2016pkusoft2017"] // 返回的数组和执行exec返回的结果一致 return '0000'; }); console.log(str); // pkusoft0000pkusoft0000
replace application
var str = '20171001'; var arr = ["零","壹","贰","叁","肆","伍","陆","柒","捌","玖"]; str = str.replace(/\d/g,function () { var num = arguments[0]; // 把捕获的内容,作为数组的下标 return arr[num]; }); console.log(str); // 贰零壹柒壹零零壹
The above content is Regular replacement tutorial of replace, I hope it can help everyone.
Related recommendations:
php Detailed explanation of string regular replacement function preg_replace
Share two regular replacement functions implemented in JS and C# Example
Detailed explanation of JS regular replacement method to remove spaces
The above is the detailed content of Regular replacement of replace. For more information, please follow other related articles on the PHP Chinese website!

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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

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











PyCharm is a commonly used Python integrated development environment with rich functions and shortcut keys that can help developers improve programming efficiency. In the daily programming process, mastering PyCharm's shortcut key replacement skills can help developers complete tasks more quickly. This article will introduce you to some commonly used replacement shortcut keys in PyCharm to help you easily improve your programming speed. 1.Ctrl+R replacement In PyCharm, you can use the Ctrl+R shortcut key to perform replacement operations.

Golang regular expressions use the pipe character | to match multiple words or strings, separating each option as a logical OR expression. For example: matches "fox" or "dog": fox|dog matches "quick", "brown" or "lazy": (quick|brown|lazy) matches "Go", "Python" or "Java": Go|Python |Java matches words or 4-digit zip codes: ([a-zA

PHP regular expressions are a powerful tool for text processing and conversion. It can effectively manage text information by parsing text content and replacing or intercepting it according to specific patterns. Among them, a common application of regular expressions is to replace strings starting with specific characters. We will explain this as follows

Use java's StringBuilder.replace() function to replace a specified range of characters. In Java, the StringBuilder class provides the replace() method, which can be used to replace a specified range of characters in a string. The syntax of this method is as follows: publicStringBuilderreplace(intstart,intend,Stringstr) The above method is used to replace the index star from

jQuery is a classic JavaScript library that is widely used in web development. It simplifies operations such as handling events, manipulating DOM elements, and performing animations on web pages. When using jQuery, you often encounter situations where you need to replace the class name of an element. This article will introduce some practical methods and specific code examples. 1. Use the removeClass() and addClass() methods jQuery provides the removeClass() method for deletion

PyCharm is a powerful Python integrated development environment with rich functions and tools that can greatly improve development efficiency. Among them, the replacement function is one of the functions frequently used in the development process, which can help developers quickly modify the code and improve the code quality. This article will introduce PyCharm's replacement function in detail, combined with specific code examples, to help novices better master and use this function. Introduction to the replacement function PyCharm's replacement function can help developers quickly replace specified text in the code

In Python, we can replace one word with another word in Excel using a third-party Python library called openpyxl. Microsoft Excel is a useful tool for managing and analyzing data. Using Python, we can automate some Excel data management tasks. In this article, we will learn how to replace a word in Excel using Python. Before installing openpyxl to replace Word in Excel, we need to install the openpyxl library in the system using the Python package manager. To install openpyxl, enter the following command in the terminal or command prompt. Pipinst

PyCharm is an integrated development environment popular among programmers. It provides powerful functions and tools to make programming more efficient and convenient. In PyCharm, reasonable setting and replacement of shortcut keys is one of the keys to improving programming efficiency. This article will introduce how to replace shortcut keys in PyCharm to make programming more convenient. 1. Why should we replace shortcut keys? In PyCharm, shortcut keys can help programmers quickly complete various operations and improve programming efficiency. However, everyone has different habits, and some people may
