Skip to main content
added 497 characters in body
Source Link
James Coyle
  • 10.5k
  • 2
  • 42
  • 49

How about \d([:.]\d?)?? That should handle all the cases you mentioned.

Or \d+([:.]\d*?)? if you want to accept more than one digit in each number.

To only match if exact simply add ^ to the start and $ to the end of the regular expression. Example:

const regex = /^\d+([:.]\d*?)?$/ console.log(regex.test('2')) console.log(regex.test('2:')) console.log(regex.test('2.')) console.log(regex.test('2:3')) console.log(regex.test('2.3')) console.log(regex.test('12:23')) console.log(regex.test('23.34')) console.log(regex.test('2:3:4')) console.log(regex.test('2.3.4'))

How about \d([:.]\d?)?? That should handle all the cases you mentioned.

Or \d+([:.]\d*?)? if you want to accept more than one digit in each number.

How about \d([:.]\d?)?? That should handle all the cases you mentioned.

Or \d+([:.]\d*?)? if you want to accept more than one digit in each number.

To only match if exact simply add ^ to the start and $ to the end of the regular expression. Example:

const regex = /^\d+([:.]\d*?)?$/ console.log(regex.test('2')) console.log(regex.test('2:')) console.log(regex.test('2.')) console.log(regex.test('2:3')) console.log(regex.test('2.3')) console.log(regex.test('12:23')) console.log(regex.test('23.34')) console.log(regex.test('2:3:4')) console.log(regex.test('2.3.4'))

edited body
Source Link
James Coyle
  • 10.5k
  • 2
  • 42
  • 49

How about \d([:.]\d?)?? That should handle all the cases you mentioned.

Or \d+([:.]\d+]\d*?)? if you want to accept more than one digit in each number.

How about \d([:.]\d?)?? That should handle all the cases you mentioned.

Or \d+([:.]\d+?)? if you want to accept more than one digit in each number.

How about \d([:.]\d?)?? That should handle all the cases you mentioned.

Or \d+([:.]\d*?)? if you want to accept more than one digit in each number.

Source Link
James Coyle
  • 10.5k
  • 2
  • 42
  • 49

How about \d([:.]\d?)?? That should handle all the cases you mentioned.

Or \d+([:.]\d+?)? if you want to accept more than one digit in each number.