D
D
Di Ma2018-07-24 00:39:36
Regular Expressions
Di Ma, 2018-07-24 00:39:36

Add zero before single digit?

Hello!
Row options

2:5
4:15
21:26
14:7

You need to select all single digits, and add a zero in front of them to get
02:05
04:15
21:26
14:07

/(^|\D)(\d)($|\D)/, "$10$2$3"Such an expression corrects if only one of the numbers is non-zero. And when both numbers, then only the first number is corrected.
How to fix?
https://regex101.com/r/46zqqu/1

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
DevMan, 2018-07-24
@dimentimor

все гораздо проще:
паттерн - /\b(\d)\b/g
заменa - 0$1
https://regex101.com/r/46zqqu/2

0
0xD34F, 2018-07-24
@0xD34F

Как исправить?

Yes, obviously - stop using regular expressions:
[
  '2:5',
  '4:15',
  '21:26',
  '14:7',
].map(n => n.split(':').map(n => n.padStart(2, '0')).join(':'))

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question