Home > Software design >  Regex having "." (no other special characters) between text it can have many dots with tex
Regex having "." (no other special characters) between text it can have many dots with tex

Time:08-22

so I am looking for a regex which disallows two consecutive "." but it can have many. Also the string should not start or end with ".".

I've written a regex but it allows multiple dots consecutively.

new RegExp("^[0-9a-z].*[0-9a-z]$", "i"),

valid examples

"abc.def.hun.asasdsd"
"asasaasass.l"
"p.asasasas"

invalid examples

".asassa"
"sasas..sasas.sas"
"asas.sasas.sasas."
"sasas...sasasdsd.sasass"

CodePudding user response:

You may use this regex:

/^[a-z\d](?!.*\.\.)(?:.*[a-z\d])?$/gmi

RegEx Demo

Explanation:

  • ^[a-z\d]: Match alphanumeric char at start
  • (?!.*\.\.): Negative lookahead to fail the match if 2 consecutive dots are present anywhere
  • (?:.*[a-z\d])?: Match anything followed by alphanumeric char. Make this an optional group match
  • $: End

CodePudding user response:

You can use this command, to start with ^[0-9a-z] with the number of letters, and then groups of dot with letters and numbers but not two sequential dots.

const result = new RegExp("^[0-9a-z] ?(\.[0-9a-z] )*$", "i").test('one.dsfsdf.');

console.log(result)

CodePudding user response:

Perhaps this works for your case?

^[0-9a-z] (\.[0-9a-z] )*$

Regex101

  • Related