-1

I have the following code to match:

String(s) in which . is not present consecutively, it can be alternatively

let strArr = [
  '#foo3.5', // true
  '#bar34..34', // false
  '.', // true
  '#ipv4-1.1.1.1' // true
];

const re = /^([^.]*\.[^.]*)+$/;

strArr.map(
  (val, idx) => console.log(`${idx}: ${re.test(val)}`)
);

But, the above code also matches #bar34..34, which is not desired. I tried changing the * meta-character in my pattern to +, but then, it fails to match . and #ipv4-1.1.1.1 strings.

Also, I want my regex to be small, because it is a part of a very long regex (you can suppose it an Email ID regex). So, what should be the required regex?

vrintle
  • 5,501
  • 2
  • 16
  • 46

1 Answers1

1

Match a whole string with no consecutive dots:

/^(?!.*\.\.).*$/
Amadan
  • 191,408
  • 23
  • 240
  • 301