1

I am trying to create a regular expression that turns a string like "5+.34+3", into "5+0.34+3". Basically I am trying to add a leading zero in front of all decimals that DON'T have a leading zero already.


Some Examples

Input Output Change
".34" "0.34" (added zero in front of decimal)
"1.45" "1.45" (No change)
"66+.33+22+.32" "66+0.33+22+0.32" (added zero in front of all decimals without one
"5+.22" "5+0.22" (added zero in front of .22)

Thank you so much for trying to figure this out!

(If there is another question on stack overflow like this one, please let me know so I can take this question down)

Promaster
  • 162
  • 1
  • 12
  • 1
    FYI: https://stackoverflow.com/a/14345184/1207049 – marekful Feb 09 '21 at 16:55
  • Please visit [help], take [tour] to see what and [ask]. Do some research, search for related topics on SO; if you get stuck, post a [mcve] of your attempt, noting input and expected output, preferably in a [Stacksnippet](https://blog.stackoverflow.com/2014/09/introducing-runnable-javascript-css-and-html-code-snippets/) – mplungjan Feb 09 '21 at 16:58
  • 2
    `your_string.replace(/(^|\D)\.(\d+)/g, "$10.$2")` – CRice Feb 09 '21 at 17:02
  • In response to Maciej Los, I am creating a calculator that takes a string as input and returns another string as the answer. – Promaster Feb 09 '21 at 17:05

2 Answers2

4

One wants to replace any decimal separator which is not prepended/prefixed by a digit character. Thus, a valid approach was to use replace running a regex which features a negative lookbehind and which does capture the character of the decimal separator.

The regex looks like this ... /(?<!\d)(\.)/g ... and reads like that ...

  • / ... /g ... apply the pattern globally by ...
  • / ... (\.)/g ... capturing the intended separator character ...
  • / (?<!\d) ... /g ... which is not (<!) prepended by a single digit (\d) ...

const sample = `
  .34
  1.45
  66+.33+22+.32
  5+.22 5+0.22 5+.22`;

const regXNonLeadingDigitDecimalSeparator = (/(?<!\d)(\.)/g);

console.log(
  'original ...',
  sample
);
console.log(
  'sanitized ...',
  sample.replace(regXNonLeadingDigitDecimalSeparator, '0$1')
);

console.log(
  'some other separator sample ...\n',
  '0,45; ,78; 0,78; 0.9; .9 =>',
  '0,45; ,78; 0,78; 0.9; .9'.replace((/(?<!\d)(,)/g), '0$1')
)
.as-console-wrapper { min-height: 100%!important; top: 0; }
Peter Seliger
  • 11,747
  • 3
  • 28
  • 37
  • 1
    Since I'm always eager to learn, the downvote could come with a comment about where the approach needs to be improved. – Peter Seliger Feb 09 '21 at 23:24
  • I concur. Some people should not be allowed to vote or downvotes should be more expensive without a comment. I assume the downvoter thought we should not answer this question because of lack of effort from the asker's side and we should be punished for our wish to help. Anyway, I voted you up. – mplungjan Feb 10 '21 at 06:23
3

Alternative to a regex with the possibility to manipulate even more

const floats = [".34", "1.45", "66+.33+22+.32", "5+.22"].map(str => {
 return  str.split('+').map(part => part.startsWith(".") ? "0"+part : part).join('+')
})
console.log(floats)
mplungjan
  • 169,008
  • 28
  • 173
  • 236