Home Backend Development PHP Tutorial Regular replacement of replace

Regular replacement of replace

Dec 02, 2017 am 09:32 AM
replace replace regular

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
Copy after login

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
Copy after login

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
Copy after login

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
Copy after login

replace application

var str = '20171001';
var arr = ["零","壹","贰","叁","肆","伍","陆","柒","捌","玖"];
str = str.replace(/\d/g,function () {
 var num = arguments[0]; // 把捕获的内容,作为数组的下标
 return arr[num];
});
console.log(str); // 贰零壹柒壹零零壹
Copy after login

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!

Statement of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Master PyCharm replacement shortcut keys in 5 minutes and easily increase your programming speed! Master PyCharm replacement shortcut keys in 5 minutes and easily increase your programming speed! Feb 22, 2024 am 10:57 AM

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.

How to replace a string starting with something with php regular expression How to replace a string starting with something with php regular expression Mar 24, 2023 pm 02:57 PM

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

How to match multiple words or strings using Golang regular expression? How to match multiple words or strings using Golang regular expression? May 31, 2024 am 10:32 AM

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

Replace the class name of an element using jQuery Replace the class name of an element using jQuery Feb 24, 2024 pm 11:03 PM

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 Beginner's Guide: Comprehensive Analysis of Replacement Functions PyCharm Beginner's Guide: Comprehensive Analysis of Replacement Functions Feb 25, 2024 am 11:15 AM

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

Use java's StringBuilder.replace() function to replace a specified range of characters Use java's StringBuilder.replace() function to replace a specified range of characters Jul 24, 2023 pm 06:12 PM

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

How to use regular expressions to remove Chinese characters in php How to use regular expressions to remove Chinese characters in php Mar 03, 2023 am 10:12 AM

How to remove Chinese in PHP using regular expressions: 1. Create a PHP sample file; 2. Define a string containing Chinese and English; 3. Use "preg_replace('/([\x80-\xff]*)/i', '',$a);" The regular method can remove Chinese characters from the query results.

How to use regular matching to remove html tags in php How to use regular matching to remove html tags in php Mar 21, 2023 pm 05:17 PM

In this article, we will learn how to remove HTML tags and extract plain text content from HTML strings using PHP regular expressions. To demonstrate how to remove HTML tags, let's first define a string containing HTML tags.

See all articles