3

I did the work and wrote the following regex:

/^([0-9.]+)$/

This is satisfying for the following conditions:

123.123.123.132
123123213123

Now I need add one more feature for this regex that it can have one alphabet in the Phone Number like

123.a123.b123.123

but not

123.aa1.bb12

I tried with

/^([0-9.]+\w{1})$/

It can contain only one alphabet between the .(dot) symbol. Can someone help me on this !!!

Thanks in Advance !!

Pavan
  • 543
  • 4
  • 16
  • 3
    a few remarks: Java and JavaScript are not the same, they are not even related. If you want us to help you figure out where your code is going wrong, show us your code. If you haven't written any yet, start with that. Don't expect us to do it for you. – Stultuske Feb 22 '19 at 07:21
  • What about `123.1a23.1b23.123`? – Nicholas K Feb 22 '19 at 07:27
  • @NicholasK a single character in between dot is a right one. So the regex should satisfy that condition as well. – Pavan Feb 22 '19 at 07:31
  • this might help https://stackoverflow.com/q/4338267/6631280 – Ravi Makwana Nov 05 '19 at 06:40

1 Answers1

2

The pattern that you use ^([0-9.]+)$ uses a character class which will match any of the listed characters and repeats that 1+ times which will match for example 123.123.123.132.

This is a bit of a broad match and does not take into account the position of the matched characters.

If your values starts with 1+ digits and the optional a-z can be right after the dot, you might use:

^\d+(?:\.[a-zA-Z]?\d+)*$

Explanation

  • ^ Start of the string
  • \d+ Match 1+ digits
  • (?: Non capturing group
    • \.[a-zA-Z]?\d+ Match a dot followed by an optional a-zA-Z and 1+ digits
  • )* Close group and repeat 0+ times
  • $ End of the string

See the regex101 demo

The fourth bird
  • 154,723
  • 16
  • 55
  • 70