Home > Web Front-end > JS Tutorial > body text

Detailed explanation of regular expression usage in Replace

php中世界最好的语言
Release: 2018-03-29 11:20:50
Original
1902 people have browsed it

This time I will bring you the regular expressions in Replacedetailed explanation of usage, what are the precautions for using regular expressions in Replace, the following is a practical case, let’s take a look one time.

replace: Replace the original characters with new characters

1. String replacement of replace

var str = 'pku2016pku2017';
str = str.replace('pku', 'pkusoft');
console.log(str); // pkusoft2016pku2017
Copy after login

Without using regular expressions, only Can 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

Firstly, it is the same as exec capture. Everything that matches our regular pattern is captured, and then the captured content is replaced 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 functionHow many 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. The return 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

Group capture of replacement

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

Application of replacement

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

I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!

Recommended reading:

The relationship between lastIndex and regular expressions

Match the regular expressions commented in js

Detailed explanation of the use of the \D metacharacter (equivalent to "[^0-9]") in regular expressions

The above is the detailed content of Detailed explanation of regular expression usage in Replace. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template