繁体   English   中英

如何使用正则表达式替换特定符号后的最新字符

[英]how to replace latest character after a specific sign using regex

我的字符串是hello-word-the-code-123 ,我想将其更改为hello-word-the-code-x (使用正则表达式将 123 替换为 x)

我试过hello-word-the-code-123.replace(/\-\d$/, '-' + i)但它给了我hello-word-the-code-123

在最后一个-之后您只匹配一个数字,但您的输入有 3 位数字。 使用\d+匹配任意数量的数字。

 let i = 'x'; console.log('hello-word-the-code-123'.replace(/-\d+$/, '-' + i));

没必要逃跑- 除非它在[]内部,否则它没有特殊含义。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM