正则表达式在数字之间替换 [英] Regex replace between digits
本文介绍了正则表达式在数字之间替换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下时间码要替换:
I have the following timecode I want to do a replace on:
00 04 02 01 -- > 00:04:02:01
如何用冒号替换两个数字之间的空格?我想出了 \d\s\d
,但这显然也包括数字.
How would I replace the space between the two digits with a colon? I have come up with \d\s\d
, but that obviously includes the digits as well.
推荐答案
捕捉你的数字,然后在替换时使用它们:
Capture your digits, then use them when replacing:
(\d)\s(?=\d)
替换为:
$1:
<小时>
以下示例使用 JavaScript:
The following example uses JavaScript:
var str = '00 04 02 01';
str = str.replace(/(\d)\s(?=\d)/g, '$1:');
这是小提琴:http://jsfiddle.net/7arUa/
这篇关于正则表达式在数字之间替换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文