search
HomeWeb Front-endJS TutorialRegular Expressions in Replace_Regular Expressions
Regular Expressions in Replace_Regular ExpressionsJan 05, 2018 am 09:08 AM
replaceexpression

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!

Statement
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
MySQL中如何使用REPLACE函数替换字符串中的指定部分MySQL中如何使用REPLACE函数替换字符串中的指定部分Jul 25, 2023 pm 01:18 PM

MySQL是一种常用的关系型数据库管理系统,它提供了多种函数来处理和操作数据。其中,REPLACE函数是用来替换字符串中的指定部分内容的。在本文中,将介绍如何在MySQL中使用REPLACE函数进行字符串替换,并通过代码示例来演示其用法。首先,我们来了解一下REPLACE函数的语法:REPLACE(str,search_str,replace_str)其

Python中的字符串查找和替换技巧有哪些?Python中的字符串查找和替换技巧有哪些?Oct 20, 2023 am 11:42 AM

Python中的字符串查找和替换技巧有哪些?(具体代码示例)在Python中,字符串是一种常见的数据类型,我们在日常编程中经常会遇到字符串的查找和替换操作。本文将介绍一些常用的字符串查找和替换技巧,并配以具体的代码示例。查找子串在字符串中查找特定的子串可以使用字符串的find()方法或者index()方法。find()方法返回子串在字符串中第一次出现的位置索

如何解决Python的表达式语法错误?如何解决Python的表达式语法错误?Jun 24, 2023 pm 05:04 PM

Python作为一种高级编程语言,易于学习和使用。一旦需要编写Python程序时,无法避免地遇到语法错误,表达式语法错误是常见的一种。在本文中,我们将讨论如何解决Python的表达式语法错误。表达式语法错误是Python中最常见的错误之一,它通常是由于错误的使用语法或缺少必要组件而导致的。在Python中,表达式通常由数字、字符串、变量和运算符组成。最常见的

在C和C++中,逗号(comma)的用法是用来分隔表达式或语句在C和C++中,逗号(comma)的用法是用来分隔表达式或语句Sep 09, 2023 pm 05:33 PM

在C或C++中,逗号“,”有不同的用途。在这里我们将了解如何使用它们。逗号作为运算符。逗号运算符是一个二元运算符,它计算第一个操作数,然后丢弃结果,然后计算第二个操作数并返回值。逗号运算符在C或C++中的优先级最低。示例#include<stdio.h>intmain(){&nbsp;&nbsp;intx=(50,60);&nbsp;&nbsp;inty=(func1(),func2());}这里60将被分配给x。对于下一条语句,将首先执行func1(

介绍C语言中的指数函数表达式介绍C语言中的指数函数表达式Feb 18, 2024 pm 01:11 PM

C语言中指数函数表达式的写法介绍及代码示例什么是指数函数指数函数是数学中一类常见的函数,可以表示为f(x)=a^x的形式,其中a为底数,x为指数。指数函数主要用于描述指数增长或指数衰减的情况。指数函数的代码示例在C语言中,我们可以使用数学库中的pow()函数来计算指数函数,下面是一个示例程序:#include

Java 中的 lambda 表达式Java 中的 lambda 表达式Jun 09, 2023 am 10:17 AM

Java中的lambda表达式随着Java8的发布,lambda表达式成为了Java开发者们最为关注和讨论的话题之一。Lambda表达式可以简化Java程序员繁琐的书写方式,同时也能够提高程序的可读性和维护性。在本文中,我们将深入探讨Java中的lambda表达式,以及它们如何在Java代码中提供更简单、更直观的编程体验。

Java中使用StringBuilder类的replace()方法替换字符串中的部分内容Java中使用StringBuilder类的replace()方法替换字符串中的部分内容Jul 24, 2023 pm 10:28 PM

Java中使用StringBuilder类的replace()方法替换字符串中的部分内容在Java编程中,字符串是一个非常重要的数据类型,经常需要对字符串进行处理和操作。而有时我们需要替换字符串中的一部分内容,以满足我们的需求。在Java中,可以使用StringBuilder类的replace()方法来实现字符串的替换操作。StringBuilder是一个可

使用lambda表达式对集合进行循环使用lambda表达式对集合进行循环Feb 19, 2024 pm 07:32 PM

lambda表达式是一种匿名函数,它可以很方便地用于遍历集合。在这篇文章中,我们将介绍如何使用lambda表达式来遍历集合,并提供具体的代码示例。在Python中,lambda表达式的语法格式如下:lambda参数列表:表达式lambda表达式的参数列表可以包含一个或多个参数,用逗号隔开。表达式是lambda函数的返回值。下面我们来看一个简单的例子,假设

See all articles

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 Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool