Ali Khazaee
Ali Khazaee

Reputation: 328

JavaScript Regex restrict _

This is my regex

^(?![^a-z])(?!.*\.\.)[a-z0-9._]+([a-z])$

Rules

everything works as like what i want, but i cannot restrict 2 o more _ in a row it has same restrict for . but does not work for _

This is online editor: https://regex101.com/r/XJXlpS/2

what is wrong ?

Upvotes: 1

Views: 44

Answers (1)

anubhava
anubhava

Reputation: 784938

You can use this regex:

/^(?![^a-z])(?!.*([_.])\1)[\w.]*[a-z]$/gmi

RegEx Demo

RegEx Breakup:

  • ^: Start
  • (?![^a-z]): Make sure first char is a letter
  • (?!.*([_.])\1): Make sure we don't have repeated underscore or dot
  • [\w.]*: Match 0+ word characters or dot
  • [a-z]: Match a letter in the end
  • $: End

Upvotes: 1

Related Questions