I want to seperate the entered string as indian money format like if i entered 123456 means, then i want to get 1,23,456

A RegExp solution would use positive lookaheads (?=pattern) to make sure the sequence is in the correct part of the String

You need to match 1 or 2 \d (digits) where

  • It is followed by zero or more groups of 2 digits (?:\d{2})*
  • Which is followed by a group of 3 digits and the end of the string \d{3}$

So you’d write a RegExp lke this /(\d\d?)(?=(?:\d{2})*\d{3}$)/, and in usage

'1234567890'.replace(/(\d\d?)(?=(?:\d{2})*\d{3}$)/g, '$1,');
// "1,23,45,67,890"

Browse More Popular Posts

Leave a Comment