当前位置:网站首页>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]/g
Look 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
边栏推荐
- Educational Codeforces Round 22 E. Army Creation
- 1003 emergency (25 points) (PAT class a)
- HDU 1097 A hard puzzle
- YOLOv5s-ShuffleNetV2
- Anhui Zhong'an online culture and tourism channel launched a series of financial media products of "follow the small editor to visit Anhui"
- node_ Exporter deployment
- BCG 使用之CBCGPTabWnd控件(相当于MFC TabControl)
- 需求开发思考
- 华为nova 10系列支持应用安全检测功能 筑牢手机安全防火墙
- Niuke Xiaobai month race 7 who is the divine Archer
猜你喜欢
水晶光电:长安深蓝SL03的AR-HUD产品由公司供应
Pointnet / pointnet++ point cloud data set processing and training
Hough transform Hough transform principle
92.(cesium篇)cesium楼栋分层
Swagger突然发癫
应用实践 | 蜀海供应链基于 Apache Doris 的数据中台建设
TCP waves twice, have you seen it? What about four handshakes?
【问题】druid报异常sql injection violation, part alway true condition not allow 解决方案
Pytoch learning (4)
New wizard effect used by BCG
随机推荐
Educational codeforces round 22 E. Army Creation
【问题】druid报异常sql injection violation, part alway true condition not allow 解决方案
c# .net mvc 使用百度Ueditor富文本框上传文件(图片,视频等)
牛客小白月赛7 E Applese的超能力
1003 emergency (25 points) (PAT class a)
Educational Codeforces Round 22 E. Army Creation
FPGA timing constraint sharing 01_ Brief description of the four steps
Actual combat simulation │ JWT login authentication
1008 elevator (20 points) (PAT class a)
Data set division
English grammar_ Noun - use
明明的随机数
HDU 1097 A hard puzzle
Mysql database basic operation -ddl | dark horse programmer
92.(cesium篇)cesium楼栋分层
Multi table operation inner join query
92. (cesium chapter) cesium building layering
Jetpack compose tutorial
Euler function
勾股数规律(任意三个数能够满足勾股定理需要满足的条件)