0

Example strings :

 2222 
 333333 
 12345 
 111
 123456789
 12345678

Expected result:

2@222
333@333
12@345
111
 123@456@789
 12@345@678

i.e. '@' should be inserted at the 4th,8th,12th etc last position from the end of the string.

I believe this can be done using replace and some other methods in JavaScript.

for validation of output string i have made the regex :

^(\d{1,3})(\.\d{3})*?$
3
  • 1
    How the last two expected results have . in it ? can you please explain ? Commented Jan 29, 2020 at 12:15
  • The question is updated again? Commented Jan 29, 2020 at 12:34
  • Yes..and sorry for that..i think the alphabets we're creating confusion.. i have added more strings too Commented Jan 29, 2020 at 12:38

3 Answers 3

1

You can use this regular expression:

/(\d)(\d{3})$/

this will match and group the first digit \d and group the last three \d{3} which are then grouped in their own group. Using the matched groups, you can then reference them in your replacement string using $1 and $2.

See example below:

const transform = str => str.replace(/(\d)(\d{3})$/, '$1@$2');

console.log(transform("2222")); // 2@222
console.log(transform("333333")); // 333@333
console.log(transform("12345")); // 12@345
console.log(transform("111")); // 111

For larger strings of size N, you could use other methods such as .match() and reverse the string like so:

const reverse = str => Array.from(str).reverse().join('');
const transform = str => {
  return reverse(reverse(str).match(/(\d{1,3})/g).join('@'));
}

console.log(transform("2222")); // 2@222
console.log(transform("333333")); // 333@333
console.log(transform("12345")); // 12@345
console.log(transform("111")); // 111
console.log(transform("123456789")); // 123@456@789
console.log(transform("12345678")); // 12@345@678

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks for the answer but actually the replacement should be at every 4,8,12 and so on if the number is big..eg 123456789 should be 123.456.789 and 12345678 should be 12.345.678
On side note:- (\d)(\d{3})$ will be enough
@CodeManiac oh, I see. Thanks :)
I have added some other strings too , i saw my question little incomplete..! 🙁
@user579757 ok, I've updated my answer so it should work with arbitrary string lengths
1

var test = [
    '111',
    '2222',
    '333333',
    '12345',
    '123456789',
    '1234567890123456'
];
console.log(test.map(function (a) {
  return a.replace(/(?=(?:\B\d{3})+$)/g, '@');
}));

1 Comment

Nice and clean, great!
1

You could match all the digits. In the replacement insert an @ after every third digit from the right using a positive lookahead.

(?=(?:\B\d{3})+$)
  • (?= Positive lookahead, what is on the right is
    • (?:\B\d{3})+ Repeat 1+ times not a word boundary and 3 digits
  • $ Assert end of string
  • ) Close lookahead

Regex demo

const regex = /^\d+$/;
["2222",
  "333333",
  "12345",
  "111",
  "123456789",
  "12345678"
].forEach(s => console.log(
  s.replace(/(?=(?:\B\d{3})+$)/g, "@")
));

2 Comments

hi.. actually alphabets and brackets and commas are not in the string .. only numbers.... i have used alphabets to just bullet the strings.
@user579757 I have updated it to match only digits.

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.