当前位置:网站首页>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
边栏推荐
- Find the nth power of 2
- Comment utiliser async awati asynchrone Task Handling au lieu de backgroundworker?
- Lenovo explains in detail the green smart city digital twin platform for the first time to solve the difficulties of urban dual carbon upgrading
- Data set division
- Mysql database basic operation -ddl | dark horse programmer
- 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?
- CANN算子:利用迭代器高效实现Tensor数据切割分块处理
- 1008 Elevator(20 分)(PAT甲级)
- Educational Codeforces Round 22 E. Army Creation
- 1007 Maximum Subsequence Sum(25 分)(PAT甲级)
猜你喜欢
TCP两次挥手,你见过吗?那四次握手呢?
C # use stopwatch to measure the running time of the program
Pointnext: review pointnet through improved model training and scaling strategies++
牛客小白月赛7 谁是神箭手
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
多表操作-外连接查询
【问题】druid报异常sql injection violation, part alway true condition not allow 解决方案
Some thoughts on whether the judgment point is located in the contour
Chrome开发工具:VMxxx文件是什么鬼
ACM组合计数入门
随机推荐
HDU 1372 & POJ 2243 Knight moves (breadth first search)
Allure of pytest visual test report
Introduction to ACM combination counting
Offset function and windowing function
"Only one trip", active recommendation and exploration of community installation and maintenance tasks
[graduation season] green ant new fermented grains wine, red mud small stove. If it snows late, can you drink a cup?
Functional interface
Niuke Xiaobai month race 7 e applese's super ability
应用实践 | 蜀海供应链基于 Apache Doris 的数据中台建设
Explicit random number
HDU 6440 2018 Chinese college student program design network competition
socket编程demo二
Niuke Xiaobai month race 7 who is the divine Archer
Online text line fixed length fill tool
Write it down once Net analysis of thread burst height of an industrial control data acquisition platform
1008 elevator (20 points) (PAT class a)
Pythagorean number law (any three numbers can meet the conditions of Pythagorean theorem)
更强的 JsonPath 兼容性及性能测试之2022版(Snack3,Fastjson2,jayway.jsonpath)
HMM隐马尔可夫模型最详细讲解与代码实现
CANN算子:利用迭代器高效实现Tensor数据切割分块处理