0

How to check if a string has 3 or more decimal points using regex. I only am wanting to use a regex pattern to solve this issue.

var string1 = "1.23432 12.123.1231"; // true
var string2 = "1.23432 12123.1231"; // false

What I thought would work but doesn't:

let regx2 = RegExp(/.{3,}/g);
  if(regx2.test(string1)){
    output = false;
  }

Any help would be much appreciated.

Travis Michael Heller
  • 1,210
  • 3
  • 16
  • 34
  • Do you want to know if any number in the string contains more than 3 decimal places, or do you want to know which number? – mhodges Mar 14 '18 at 17:13
  • 2
    Possible duplicate of [Count the number of occurrences of a character in a string in Javascript](https://stackoverflow.com/questions/881085/count-the-number-of-occurrences-of-a-character-in-a-string-in-javascript) – ctwheels Mar 14 '18 at 17:15
  • 1
    `retrun true;` might be a pain point. – Bucket Mar 14 '18 at 17:21

6 Answers6

1

An alternative is using the function match:

This approach only counts the amount of dots regardless of the current format.

This regex /(\.)/g captures the desired groups.

console.log(("1.23432 12.123.1231".match(/(\.)/g) || []).length >= 3);
console.log(("1.23432 12123.1231".match(/(\.)/g) || []).length >= 3);
Ele
  • 33,468
  • 7
  • 37
  • 75
  • @mhodges yes, however the OP stated this: *how to check if a string has 3 or more decimal points using regex.* – Ele Mar 14 '18 at 17:16
  • Yes, could be an XY Problem, though - I have offered an alternative, just in case. – mhodges Mar 14 '18 at 17:17
1

You can use match to retrieves the matches when matching a string against a regular.

function hasMoreThan3OrMore(s) {
  return s.match(/\./g).length >= 3;
}

var string1 = "1.23432 12.123.1231"; // true
var string2 = "1.23432 12123.1231"; // false

console.log(hasMoreThan3OrMore(string1));
console.log(hasMoreThan3OrMore(string2));
Eddie
  • 26,593
  • 6
  • 36
  • 58
1

An alternative to using Regex would be to use String.split() and check the length, like so:

console.log("1.23432 12.123.1231".split(".").length > 3); // true
console.log("1.23432 12123.1231".split(".").length > 3); // false
mhodges
  • 10,938
  • 2
  • 28
  • 46
0

You can remove all non . characters and them check the length

let strs = [
  "11.11 1.111.11.1111",
  "1.1.1"
]

for (let s of strs) {
  console.log(s.replace(/[^\.]/g, '').length >= 3)
}
Kevin Qian
  • 2,532
  • 1
  • 16
  • 26
0

RegExp.test() returns a boolean, so you can simply return that. Fixing your typo would also clear up your error:

var string1 = "1.23432 12.123.1231"; // true
var string2 = "1.23432 12123.1231"; // false

let regx2 = RegExp(/.{3,}/g);

console.log(regx2.test(string1); // true
console.log(regx2.test(string2); // false
Bucket
  • 7,415
  • 9
  • 35
  • 45
  • I am ending the function by returning true if the match returns true. The regex itself is not working properly by finding a string that has 3 or more decimals in it. I ended up figuring out the regex to make it work witch is /^(.*\.){3,}\d+$/g. – Travis Michael Heller Mar 14 '18 at 17:21
  • And for ending a function with the value of the match, you can simply call `return regx2.test(string1);` – Bucket Mar 14 '18 at 17:24
0

The regex I was looking for was

 let regx2 = RegExp(/^(.*\.){3,}\d+$/g);
 if(regx2.test(string2)){
   console.log('testing 3 or more . :', string1);
   output = false;
 }

I am trying to get better on my knowledge of using regex expressions and so was looking for an answer that only uses regex without using a method. Thanks for all your help, I actually didn't even think about checking if the string contains a decimal and then checking how many times it returns.

Travis Michael Heller
  • 1,210
  • 3
  • 16
  • 34