Regular Expressions in Replace_Regular Expressions
This article mainly introduces the relevant knowledge of regular expressions in Replace. The article introduces the method of replace to replace the original characters with new characters. Friends who need it can refer to it. I hope it can help everyone.
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
is not used In the case of regularity, only one character can be replaced at each execution. 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
Firstly, just like exec capture, we capture everything that matches our regular rules, 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
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
Application of replacement
var str = '20171001'; var arr = ["零","壹","贰","叁","肆","伍","陆","柒","捌","玖"]; str = str.replace(/\d/g,function () { var num = arguments[0]; // 把捕获的内容,作为数组的下标 return arr[num]; }); console.log(str); // 贰零壹柒壹零零壹
Related recommendations:
Example introduction to regular expression form validation
Using regular expressions in JS Expression to find the length of the longest continuous substring
Use regular expressions to highlight characters
The above is the detailed content of Regular Expressions in Replace_Regular Expressions. 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

MySQL is a commonly used relational database management system that provides a variety of functions to process and operate data. Among them, the REPLACE function is used to replace the specified part of the string. In this article, we will introduce how to use the REPLACE function for string replacement in MySQL and demonstrate its usage through code examples. First, let’s take a look at the syntax of the REPLACE function: REPLACE(str,search_str,replace_str).

What are the string search and replace techniques in Python? (Specific code example) In Python, strings are a common data type, and we often encounter string search and replace operations in daily programming. This article will introduce some common string search and replacement techniques, accompanied by specific code examples. To find a specific substring in a string, you can use the find() method or index() method of the string. The find() method returns the index of the first occurrence of the substring in the string.

Python, as a high-level programming language, is easy to learn and use. Once you need to write a Python program, you will inevitably encounter syntax errors, and expression syntax errors are a common one. In this article, we will discuss how to resolve expression syntax errors in Python. Expression syntax errors are one of the most common errors in Python, and they are usually caused by incorrect usage of syntax or missing necessary components. In Python, expressions usually consist of numbers, strings, variables, and operators. most common

In C or C++, the comma "," has different uses. Here we will learn how to use them. Commas as operators. The comma operator is a binary operator that evaluates the first operand, discards the result, then evaluates the second operand and returns the value. The comma operator has the lowest precedence in C or C++. Example #include<stdio.h>intmain(){ intx=(50,60); inty=(func1(),func2());} Here 60 will be assigned to x. For the next statement, func1( will be executed first

Lambda expression, as the name suggests, is an anonymous function with the arrow symbol (->) as its core. It allows you to pass blocks of code as arguments to other methods, or store them into variables for later use. Lambda expression syntax is concise and easy to understand, and it is very suitable for processing data flow and parallel computing. 1. The basic syntax of Lambda expression The basic syntax of Lambda expression is as follows: (parameter list)->{code block} Among them, the parameter list and code block are optional. If there is only one parameter, the parentheses can be omitted. If the code block is only one line, the curly braces can be omitted. For example, the following code block uses a Lambda expression to add 1 to a number: List

Introduction to how to write exponential function expressions in C language and code examples What is an exponential function? The exponential function is a common type of function in mathematics. It can be expressed in the form of f(x)=a^x, where a is the base and x is the exponent. . Exponential functions are mainly used to describe exponential growth or exponential decay. Code example of exponential function In C language, we can use the pow() function in the math library to calculate the exponential function. The following is a sample program: #include

Lambda expressions in Java With the release of Java 8, lambda expressions have become one of the most concerned and discussed topics among Java developers. Lambda expressions can simplify Java programmers' tedious writing methods, and can also improve the readability and maintainability of programs. In this article, we will take a deep dive into lambda expressions in Java and how they provide a simpler and more intuitive programming experience in Java code.

A lambda expression is an anonymous function that can be conveniently used to iterate over a collection. In this article, we will introduce how to use lambda expressions to iterate over collections, and provide specific code examples. In Python, the syntax format of a lambda expression is as follows: lambda parameter list: The parameter list of an expression lambda expression can contain one or more parameters, separated by commas. The expression is the return value of the lambda function. Let's look at a simple example below, assuming
