5

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

2
  • 1
    Replace all dots starting from the index of the first dot until the end of the string. Commented Oct 23, 2018 at 11:19
  • @thanks shilly but what I want is to handle all the dots problem as like of 2 mentioned in the problem with the single regex. I have tried this .replace(/(\d*)(\.){1,1}(\d*)(?=(\.)+)/g,'$1') which is not working Commented Oct 23, 2018 at 11:39

5 Answers 5

3

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));

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

2 Comments

@thanks Nina but can you please explain me the use of (0) at the end of replace function
it is the start value of c in a closure.
1

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);
});

1 Comment

I need to do by using regex only.
1

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);

Comments

0

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.

Comments

0

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)));

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.