Shraddha Goel
Shraddha Goel

Reputation: 897

Regex for decimal number with only single dot use replace() method

I just want if someone type 10.01.1 then it will be 10.011 & if someone type something like 10.00............1 it will become 10.001

I have tried this .replace(/(\d*)(\.){1,1}(\d*)(?=(\.)+)/g,'$1'). But it's not working for me

Upvotes: 5

Views: 687

Answers (5)

axiac
axiac

Reputation: 72196

A simple solution that uses three replace operations, without regular expressions.

Probably not the fastest but definitely the easiest to read and understand.

function repair(nb) {
  return nb.replaceAll('.', '#').replace('#', '.').replaceAll('#', '');
}

console.log(['10.01.1', '10.00............1', '100', ''].map((i) => repair(i)));

String.replaceAll() has been introduced in 2020. It does not work in older browser. However, there is a solution that works with older browsers too and uses regexps!

function repair(nb) {
  return nb.replace(/\./g, '#').replace('#', '.').replace(/#/g, '');
}

console.log(['10.01.1', '10.00............1', '100', ''].map((i) => repair(i)));

Upvotes: 0

Nina Scholz
Nina Scholz

Reputation: 386560

You could count the dots and replace onlc the firts with a dot, the rest remove the dot.

function singleDot(s) {
    return s.replace(/\./g, (c => _ => c++ ? '' : '.')(0));
}

console.log(['0.0', '0....0', '0.0...1'].map(singleDot));

Upvotes: 3

oriberu
oriberu

Reputation: 1216

If you want to do it via regex only, you can search for an alternation that captures your first period but simply matches all others and then replace all matches with the captured group. Since the captured string will be empty for the matches that aren't captured, only the first period group is replaced by itself while the other periods are deleted. See regex demo.

Search for (^\d*\.)|\. and replace with $1.

Upvotes: 0

zer00ne
zer00ne

Reputation: 43880

Try /[^\d.\s]|(?<=\.\d*)\./g;. In short, replace every character except digits 0-9, ".", and white-spaces OR any "." that is preceded by a "." and zero or more digits 0-9. The positive look-behind (?<=) ensures that any "." that has a "." before it will be replaced which means that the first "." of every group will not be replaced. Note, that every group separated by a space or new line is considered a separate number due to not replacing any \s.

Segment Description
[^\d.\s]
Replace any character BUT NOT digits 0-9, ".", and white-space
|
OR
(?<=\.\d*)\.
replace any "." that's preceded by a "." and zero or more digits 0-9

RegEx101

const rgx = /[^\d.\s]|(?<=\.\d*)\./g;
const str = `.08.2  5.880..
0.00..5.3
223...9.`;
const res = str.replace(rgx, "");

console.log(res);

Upvotes: 1

The fourth bird
The fourth bird

Reputation: 163217

You might use split and the dot as a separator:

const strings = [
  "10.01.1",
  "10.00............1",
  "100",
  ""
];
strings.forEach((s) => {
  const [head, ...rest] = s.split('.');
  console.log(rest.length ? head + '.' + rest.join('') : head);
});

Upvotes: 1

Related Questions