1

I have come up my regex in javascript ^[a-zA-Z0-9.]+([.][a-zA-Z0-9]+)$ which I am trying to filter out string.

  • No dot character at the begin nor end
  • dot character can be middle of other Alphabet letter or numbers
  • If dot character cannot be appear more than twice

These are my expected pass cases

  • foo.bar
  • f.o.o
  • foo.bar.foo
  • fo.123.321

These are my expected No-pass cases

  • .foo
  • .foo.
  • foo.
  • foo..bar
  • .foo.bar

I feel like I am very close(or not at all). How can I filter out dot repeating dot characters? Thanks in advance!

1 Answer 1

3

I think that's what you looking for:

/^([a-zA-Z0-9]+\.)+([a-zA-Z0-9]+)$/

Tests

expected pass cases

/^([a-zA-Z0-9]+\.)+([a-zA-Z0-9]+)$/.test('foo.bar') // true
/^([a-zA-Z0-9]+\.)+([a-zA-Z0-9]+)$/.test('f.o.o') // true
/^([a-zA-Z0-9]+\.)+([a-zA-Z0-9]+)$/.test('foo.bar.foo') // true
/^([a-zA-Z0-9]+\.)+([a-zA-Z0-9]+)$/.test('fo.123.321') // true

Expected No-pass cases

/^([a-zA-Z0-9]+\.)+([a-zA-Z0-9]+)$/.test('.foo') // false
/^([a-zA-Z0-9]+\.)+([a-zA-Z0-9]+)$/.test('.foo.') // false
/^([a-zA-Z0-9]+\.)+([a-zA-Z0-9]+)$/.test('foo.') // false
/^([a-zA-Z0-9]+\.)+([a-zA-Z0-9]+)$/.test('foo..bar') // false
/^([a-zA-Z0-9]+\.)+([a-zA-Z0-9]+)$/.test('.foo.bar') // false
Sign up to request clarification or add additional context in comments.

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.