当前位置:网站首页>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
边栏推荐
- Niuke Xiaobai monthly race 7 I new Microsoft Office Word document
- Socket programming demo II
- BCG 使用之CBCGPProgressDlgCtrl進度條使用
- 1007 maximum subsequence sum (25 points) (PAT class a)
- HDU 1372 & POJ 2243 Knight Moves(广度优先搜索)
- 更强的 JsonPath 兼容性及性能测试之2022版(Snack3,Fastjson2,jayway.jsonpath)
- mysql中explain语句查询sql是否走索引,extra中的几种类型整理汇总
- Is it safe to open an account at Great Wall Securities? How to open an account when buying stocks
- Reflection (I)
- Basic use of kotlin
猜你喜欢

abc229 总结(区间最长连续字符 图的联通分量计数)

Lenovo explains in detail the green smart city digital twin platform for the first time to solve the difficulties of urban dual carbon upgrading

YOLOv5s-ShuffleNetV2

TCP waves twice, have you seen it? What about four handshakes?

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

Online text line fixed length fill tool

Cbcgptabwnd control used by BCG (equivalent to MFC TabControl)

Actual combat simulation │ JWT login authentication

Swagger突然发癫

92.(cesium篇)cesium楼栋分层
随机推荐
TCP waves twice, have you seen it? What about four handshakes?
Offset function and windowing function
Niuke Xiaobai month race 7 who is the divine Archer
求2的n次方
勾股数规律(任意三个数能够满足勾股定理需要满足的条件)
Cbcgpprogressdlgctrl progress bar used by BCG
[QNX Hypervisor 2.2用户手册]6.3.1 工厂页和控制页
New wizard effect used by BCG
Wireshark network packet capture
English grammar_ Noun - use
Kotlin cycle control
Find the nth power of 2
Socket programming demo II
实战模拟│JWT 登录认证
C language - Introduction - Foundation - grammar - process control (VII)
C语言-入门-基础-语法-流程控制(七)
1005 Spell It Right(20 分)(PAT甲级)
Anhui Zhong'an online culture and tourism channel launched a series of financial media products of "follow the small editor to visit Anhui"
西门子HMI下载时提示缺少面板映像解决方案
1007 maximum subsequence sum (25 points) (PAT class a)