当前位置:网站首页>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
边栏推荐
- The explain statement in MySQL queries whether SQL is indexed, and several types in extra collate and summarize
- [graduation season] green ant new fermented grains wine, red mud small stove. If it snows late, can you drink a cup?
- Development and construction of DFI ecological NFT mobile mining system
- [QNX Hypervisor 2.2用户手册]6.3.1 工厂页和控制页
- Allure of pytest visual test report
- BCG 使用之新建向导效果
- socket编程demo二
- Educational Codeforces Round 22 E. Army Creation
- Abc229 summary (connected component count of the longest continuous character graph in the interval)
- JVM系列之对象的创建
猜你喜欢

Master the use of auto analyze in data warehouse

Development and construction of DFI ecological NFT mobile mining system

Decryption function calculates "task state and lifecycle management" of asynchronous task capability
Some thoughts on whether the judgment point is located in the contour
![[problem] Druid reports exception SQL injection violation, part always true condition not allow solution](/img/cc/160bc8ccdc378901510c1b61c3f5d3.png)
[problem] Druid reports exception SQL injection violation, part always true condition not allow solution

HMM隐马尔可夫模型最详细讲解与代码实现

Huawei Nova 10 series supports the application security detection function to build a strong mobile security firewall

C语言-入门-基础-语法-流程控制(七)

Swagger suddenly went crazy

线上数据库迁移的几种方法
随机推荐
Jetpack Compose 教程
Functional interface
BCG 使用之CBCGPTabWnd控件(相当于MFC TabControl)
牛客小白月赛7 E Applese的超能力
Stream stream
多表操作-内连接查询
1003 Emergency(25 分)(PAT甲级)
Explicit random number
HDU 1097 A hard puzzle
Anhui Zhong'an online culture and tourism channel launched a series of financial media products of "follow the small editor to visit Anhui"
1005 spell it right (20 points) (pat a)
1002. A+B for Polynomials (25)(PAT甲级)
Data set division
What should we pay attention to when doing social media marketing? Here is the success secret of shopline sellers!
Educational Codeforces Round 22 E. Army Creation
【问题】druid报异常sql injection violation, part alway true condition not allow 解决方案
JVM系列之对象的创建
数据集划分
c# .net mvc 使用百度Ueditor富文本框上传文件(图片,视频等)
黑马程序员-软件测试--09阶段2-linux和数据库-31-43修改文件权限字母发的说明,-查找链接修改文件,查找文件命令,链接文件,压缩解压方式,vi编辑器基本使用,