当前位置:网站首页>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
边栏推荐
- 勾股数规律(任意三个数能够满足勾股定理需要满足的条件)
- 数据集划分
- 水晶光电:长安深蓝SL03的AR-HUD产品由公司供应
- Socket programming demo II
- Swagger suddenly went crazy
- kotlin 基本数据类型
- Introduction to ACM combination counting
- 1002. A+b for Polynomials (25) (PAT class a)
- Cbcgptabwnd control used by BCG (equivalent to MFC TabControl)
- Pointnet / pointnet++ point cloud data set processing and training
猜你喜欢
mysql中explain语句查询sql是否走索引,extra中的几种类型整理汇总
92.(cesium篇)cesium楼栋分层
TCP两次挥手,你见过吗?那四次握手呢?
实战模拟│JWT 登录认证
[problem] Druid reports exception SQL injection violation, part always true condition not allow solution
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,
In the first month of its launch, the tourist praise rate of this campsite was as high as 99.9%! How did he do it?
Introduction to polyfit software
Cann operator: using iterators to efficiently realize tensor data cutting and blocking processing
随机推荐
How to use async Awati asynchronous task processing instead of backgroundworker?
1011 World Cup Betting (20 分)(PAT甲级)
西门子HMI下载时提示缺少面板映像解决方案
数据集划分
Multi table operation - external connection query
求2的n次方
Kotlin inheritance
@Data source connection pool exhaustion caused by transactional abuse
[QNX Hypervisor 2.2用户手册]6.3.1 工厂页和控制页
需求开发思考
Reflection (I)
Offset function and windowing function
Niuke Xiaobai month race 7 e applese's super ability
牛客小白月赛7 I 新建 Microsoft Office Word 文档
Data set division
92. (cesium chapter) cesium building layering
1009 Product of Polynomials(25 分)(PAT甲级)
Dark horse programmer - software testing - stage 08 2-linux and database-23-30-process port related, modify file permissions, obtain port number information, program and process related operations, Li
mysql中explain语句查询sql是否走索引,extra中的几种类型整理汇总
1003 emergency (25 points) (PAT class a)