


Detailed explanation of the regular expression replace example of the string object method in js
本篇文章主要介绍了js中string之正则表达式replace方法详解,replace方法是javascript涉及到正则表达式中较为复杂的一个方法,严格上说应该是string对象的方法。
replace方法是javascript涉及到正则表达式中较为复杂的一个方法,严格上说应该是string对象的方法。只不过牵扯到正则的时候比较多一些。需要我们灵活的使用。
语法: stringObj.replace(regexp/substr,replacement);
第一个参数:必需。字符串中要替换的子串或正则RexExp;
第二个参数:必需,一个字符串值,规定了替换文本或生成替换文本的函数。
返回值:注意它的返回值是一个新的字符串,并没有更改原有字符串,是用 replacement 替换了 regexp 的第一次匹配或所有匹配之后得到的。
所以根据它的参数的不同分为很多种情况,以下一一对各种情况加以分析:
NO.1 两个参数都是字符串
var str1 = '这是一段原始文本,需要替换的内容"这要替换"!'; var newStr = str1.replace('这要替换','need replace'); console.log( newStr ); //输出: 这是一段原始文本,需要替换的内容"need replace"!
上面的例子中第二个参数字符串'need replace',替换掉了第一个参数字符串'这要替换'。这是最简单的一种形式。
NO.2 第一个参数是正则,第二个参数是字符串
var str2 = '这是一段原始文本,需要替换的内容"ac这要替换bb"!'; var newStr = str2.replace( /([a-z])+/g,'qqq' ); console.log( newStr ); //输出: 这是一段原始文本,需要替换的内容"qqq这要替换qqq"!
上面的例子字符串'qqq',替换了正则匹配的内容。如果 regexp 具有全局标志 g,那么 replace() 方法将替换所有匹配的子串。否则,它只替换第一个匹配子串。
NO.3 第一个参数是正则,第二个参数是带$符的字符串
var str3 = '这是一段原始文本,"3c这要替换4d"!'; var newStr = str3.replace( /([0-9])([a-z])/g,"$1" ); console.log( newStr ); //输出: 这是一段原始文本,"3这要替换4"!';
上面的例子,$1表示regexp中的第一个子表示即([0-9])匹配单个数字,同理若是$2则表示第二个子表示即([a-z]);所以,'3c'这个匹配到的整体被第一个子表示说表示的'3'替换,'4d'被第一个子表示匹配的数字'4'所替换。其他几个同理可得:
(/([0-9])([a-z])/g,”$2″)—>////输出: 这是一段原始文本,”c这要替换d”!'; (3c和4d被相应的第二个子表示匹配出来的c和d替换)(/([0-9])([a-z])/g,”$'”)—>////输出: 这是一段原始文本,”这要替换d”!这要替换”!”!'; (3c被3c右侧文本替换,4d右侧是”!替换,所以出现俩次)
NO.4 第一个参数是正则,第二个参数函数
var str4 = '这是一段原始文本,需要替换的内容"aa这要bbb替换ccccc"!'; var newStr = str4.replace( /[a-z]+/g,function ($0){ var str = ''; for (var i = 0; i < $0.length; i++) { str += '*'; }; return str; } ); console.log( newStr ); //这是一段原始文本,需要替换的内容"**这要***替换*****"!
上面的例子函数的第一个参数为匹配的regexp的整体,根据长度函数返回值为相应替换的文本;
NO.5 第一个参数是正则且有子表达式,第二个参数函数且带有多个参数
var str5 = '这是一段原始文本,需要替换的内容"3c这要替换4d"!'; var newStr = str5.replace( /([0-9])([a-z])/g,function (arg1,arg2,arg3,arg4,arg5){ console.log( arg1 ); console.log( arg2 ); console.log( arg3 ); console.log( arg4 ); console.log( arg5 ); } );
输出:
3c
3
c
17
这是一段原始文本,需要替换的内容"3c这要替换4d"!
4d
4
d
23
这是一段原始文本,需要替换的内容"3c这要替换4d"!
上面的例子第一个参数arg1表示匹配的整体,arg2表示第一个子表达式,arg3表示第二个子表达式,接下来的参数arg4是一个整数,声明了表示子匹配在 stringObject 中出现的位置。最后一个参数是 stringObject 本身。
以上就是replace方法各种可能的情况。确实是一个需要深入理解的方法,不过确实也很强大的一个方法,值得深入研究!
The above is the detailed content of Detailed explanation of the regular expression replace example of the string object method in js. For more information, please follow other related articles on the PHP Chinese website!

JavaScript is widely used in websites, mobile applications, desktop applications and server-side programming. 1) In website development, JavaScript operates DOM together with HTML and CSS to achieve dynamic effects and supports frameworks such as jQuery and React. 2) Through ReactNative and Ionic, JavaScript is used to develop cross-platform mobile applications. 3) The Electron framework enables JavaScript to build desktop applications. 4) Node.js allows JavaScript to run on the server side and supports high concurrent requests.

Python is more suitable for data science and automation, while JavaScript is more suitable for front-end and full-stack development. 1. Python performs well in data science and machine learning, using libraries such as NumPy and Pandas for data processing and modeling. 2. Python is concise and efficient in automation and scripting. 3. JavaScript is indispensable in front-end development and is used to build dynamic web pages and single-page applications. 4. JavaScript plays a role in back-end development through Node.js and supports full-stack development.

C and C play a vital role in the JavaScript engine, mainly used to implement interpreters and JIT compilers. 1) C is used to parse JavaScript source code and generate an abstract syntax tree. 2) C is responsible for generating and executing bytecode. 3) C implements the JIT compiler, optimizes and compiles hot-spot code at runtime, and significantly improves the execution efficiency of JavaScript.

JavaScript's application in the real world includes front-end and back-end development. 1) Display front-end applications by building a TODO list application, involving DOM operations and event processing. 2) Build RESTfulAPI through Node.js and Express to demonstrate back-end applications.

The main uses of JavaScript in web development include client interaction, form verification and asynchronous communication. 1) Dynamic content update and user interaction through DOM operations; 2) Client verification is carried out before the user submits data to improve the user experience; 3) Refreshless communication with the server is achieved through AJAX technology.

Understanding how JavaScript engine works internally is important to developers because it helps write more efficient code and understand performance bottlenecks and optimization strategies. 1) The engine's workflow includes three stages: parsing, compiling and execution; 2) During the execution process, the engine will perform dynamic optimization, such as inline cache and hidden classes; 3) Best practices include avoiding global variables, optimizing loops, using const and lets, and avoiding excessive use of closures.

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

Python and JavaScript have their own advantages and disadvantages in terms of community, libraries and resources. 1) The Python community is friendly and suitable for beginners, but the front-end development resources are not as rich as JavaScript. 2) Python is powerful in data science and machine learning libraries, while JavaScript is better in front-end development libraries and frameworks. 3) Both have rich learning resources, but Python is suitable for starting with official documents, while JavaScript is better with MDNWebDocs. The choice should be based on project needs and personal interests.


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

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Dreamweaver Mac version
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version
Useful JavaScript development tools