1

I have a number that's at least 7 digits long. Typical examples: 0000123, 00001234, 000012345

I want to transform them so that they become respectively: 01:23, 12:34, 23:45

Which mean replacing the whole string by the last 4 characters and putting a colon in the middle.

I can get the last 4 digits with (\d{4})$ And I can get 2 groups with this: (\d{2})(\d{2})$

With the last option, on a string 0000123 $1:$2 match gives me 00001:23 where I want 01:23

I replace the string like so:

newVal = val.replace(/regex/, '$1:$2');
3
  • 2
    No need for an RE: time = val.substr(val.length - 4, 2) + ":" + val.substr(-2); ? Commented Jul 28, 2017 at 13:03
  • @gyc which language.? Commented Jul 28, 2017 at 13:03
  • 1
    @AlexK.: Since you didn't post an answer, I posted a CW with that, as it frankly seems the simplest, most straight-forward way to do it. But if you decide to post it, let me know and I'll delete it. Commented Jul 28, 2017 at 13:11

4 Answers 4

2

You need to match the beginning digits with \d* (or with just .* if there can be anything):

var val = "0001235";
var newVal = val.replace(/^\d*(\d{2})(\d{2})$/, '$1:$2');
console.log(newVal);

Pattern details:

  • ^ - start of string
  • \d* - 0+ digits (or .* will match any 0+ chars other than line break chars)
  • (\d{2}) - Group 1 capturing 2 digits
  • (\d{2}) - Group 2 capturing 2 digits
  • $ - end of string.
Sign up to request clarification or add additional context in comments.

2 Comments

Or as the digit string is known to be at least 7 digits you could match it with \d{3,}(\d{2})(\d{2}) and then any shorter digit strings will be skipped.
Yes, that is true.
2

As Alex K. said, no need for a regular expression, just extract the parts you need with substr:

val = val.substr(-4, 2) + ":" + val.substr(-2);

Note that when the starting index is negative, it's from the end of the string.

Example:

function update(val) {
  return val.substr(-4, 2) + ":" + val.substr(-2);
}
function test(val) {
  console.log(val + " => " + update(val));
}
test("0000123");
test("0001234");
test("000012345");

3 Comments

Correct, this is a good solution. In my particular case there are other regex manipulations above my code and I feel the regex version will be helpful if the parsing becomes more complex later in the project.
@gyc: Fair enough, but frankly, I'd introduce it later in the project if it becomes necessary, and keep it simple until then.
Yes definitely. Thanks
1

You could throw the first characters away and the replace only the last matched parts.

console.log('00000001234'.replace(/^(.*)(\d{2})(\d{2})$/, '$2:$3'));

Comments

1

Use this regex: ^(\d+?)(\d{2})(\d{2})$:

var newVal = "0000123".replace(/^(\d+?)(\d{2})(\d{2})$/, '$2:$3');
console.log(newVal);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.