Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use regular expressions in Replace

2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article shows you how to use regular expressions in Replace. The content is concise and easy to understand. It will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.

Replace: replace the original characters with new ones

1. String substitution of replace

Var str = 'pku2016pku2017';str = str.replace (' pku', 'pkusoft'); console.log (str); / / pkusoft2016pku2017

In the case of not using rules, only one character can be replaced at each execution, and each execution is replaced from 0, there is repetition, and all cannot be replaced.

2. Regular substitution of replace

Str = str.replace (/ pku/g, 'pkusoft'); / / use regular global matching console.log (str); / / pkusoftsoft2016pkusoft2017

First, like the exec capture, capture everything that matches our regularity, and then replace the capture with the new content we need to replace.

/ pku/g captures everything that can match in str according to this rule, and then replaces it with 'pkusoft''.

If the second parameter of replace is a function

1. How many times an anonymous function is executed depends on how many times the rule can be captured in a string

two。 Each time an anonymous function is executed, the arguments value is similar to what is captured through exec.

3. The return value of return is the content that needs to be replaced

Str = str.replace (/ pku/g, function () {console.log (arguments); / / first execution: ["pku", 0, "pku2016pku2017"] / / first execution: ["pku", 7, "pku2016pku2017"] / / the array returned is the same as the result returned by exec); console.log (str); / / pkusoftsoft2016pkusoft2017

Packet capture of replace

Str = str.replace (/ (\ d +) / g, function () {/ / console.log (arguments); / / first execution: ["2016", "2016", 7, "pkusoft2016pkusoft2017"] / / first execution: ["2017", "2017", 18, "pkusoft2016pkusoft2017"] / / the array returned is the same as the result returned by exec;}); console.log (str); / / pkusoft0000pkusoft0000

Application of replace

Var str = '20171001 traumatic arr = ["zero", "one", "II", "3", "restaurant", "Wu", "Lu", "Qi", "Qi", "nine"]; str = str.replace (/\ d return arr function () {var num = arguments [0]; / / take the captured contents as the subscript of the array return arr [num];}); console.log (str) / / the above is how to use regular expressions in Replace. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report