当前位置:网站首页>Regular replacement [JS, regular expression]
Regular replacement [JS, regular expression]
2022-07-04 20:04:00 【qq_ twenty-two million eight hundred and forty-one thousand thr】
Problem description
Representation number _ Niuke Tiba _ Cattle from (nowcoder.com)
The title requires that all figures be replaced with * Numbers *
Originally, I intended to use this question routinely c++ To write , But in the process of writing, I suddenly realized
Replace all numbers , Don't you just find all the numbers and replace them ? Then you can use Regular expressions 了
With this idea , I'll find the corresponding regular expression
Solve the code
var value = readline();
console.log(value.replace(/(\d+)/g, (number) => {
return `*${
number}*`;
} ))
you 're right , Just two sentences …… I was also a little shocked when I submitted the answer successfully ……
The course of thinking
In fact, when I first got this topic , I already knew that it can be replaced by regularization , But it's just a little short , Let me solve this problem How to not just replace , Instead, add characters and replace
So I found that it can be used Function to replace
1. Get all the numbers
The first step is to get all the numbers through regular expressions ,/(\d+)/g
(\d+): Match one or more numbers/g: The global matching , And replaceAll similar
In fact, I know how to replace numbers
/[0-9]/g, It can also match all the numbers , But it is not the result we want .
adopt/[0-9]/gLook for numbers , Only one number can be found , Because only one number is satisfied 0-9 On this condition
And by(\d+)Then you can Match to consecutive numbers
2. Replace assignment
After finding all the numbers , The second step is how to replace the number with * Numbers * 了 .
Directly through the function to deal with
(number) => {
return `*${
number}*`;
}
Because the first step , We have found all the consecutive numbers , Then we can connect these figures , After processing , And back out
number: All the numbers- With
* Numbers *returns
边栏推荐
- Jetpack Compose 教程
- Niuke Xiaobai month race 7 who is the divine Archer
- TCP waves twice, have you seen it? What about four handshakes?
- The explain statement in MySQL queries whether SQL is indexed, and several types in extra collate and summarize
- 公司要上监控,Zabbix 和 Prometheus 怎么选?这么选准没错!
- Multi table operation inner join query
- 解密函数计算异步任务能力之「任务的状态及生命周期管理」
- [QNX hypervisor 2.2 user manual]6.3.1 factory page and control page
- ACM组合计数入门
- 黑马程序员-软件测试--07阶段2-linux和数据库-09-24-linux命令学习步骤,通配符,绝对路径,相对路径,文件和目录常用命令,文件内容相关操作,查看日志文件,ping命令使用,
猜你喜欢

Cann operator: using iterators to efficiently realize tensor data cutting and blocking processing

Cbcgptabwnd control used by BCG (equivalent to MFC TabControl)

Dark horse programmer - software testing - 09 stage 2-linux and database -31-43 instructions issued by modifying the file permission letter, - find the link to modify the file, find the file command,

c# .net mvc 使用百度Ueditor富文本框上传文件(图片,视频等)

C # use stopwatch to measure the running time of the program

Actual combat simulation │ JWT login authentication

What should we pay attention to when doing social media marketing? Here is the success secret of shopline sellers!

Pythagorean number law (any three numbers can meet the conditions of Pythagorean theorem)

HMM hidden Markov model and code implementation

BCG 使用之CBCGPProgressDlg进度条使用
随机推荐
Abc229 summary (connected component count of the longest continuous character graph in the interval)
多表操作-内连接查询
ACM组合计数入门
C language - Introduction - Foundation - grammar - process control (VII)
What should we pay attention to when doing social media marketing? Here is the success secret of shopline sellers!
"Only one trip", active recommendation and exploration of community installation and maintenance tasks
Explicit random number
Personal thoughts on Architecture Design (this article will be revised and updated continuously later)
Online sql to excel (xls/xlsx) tool
1011 World Cup Betting (20 分)(PAT甲级)
Online text line fixed length fill tool
92. (cesium chapter) cesium building layering
Kotlin basic data type
Double colon function operator and namespace explanation
【毕业季】绿蚁新醅酒,红泥小火炉。晚来天欲雪,能饮一杯无?
Stream stream
明明的随机数
YOLOv5s-ShuffleNetV2
应用实践 | 蜀海供应链基于 Apache Doris 的数据中台建设
1002. A+b for Polynomials (25) (PAT class a)