3

I have a data of day and I need to check if it contain only 0. How I can check if string contain only 0? I want to do it with regular expressions.

Aymand Osanes
  • 677
  • 1
  • 6
  • 9

3 Answers3

13
/^0*$/.test(subject)

returns True for a string that contains nothing but (any number of, including 0) zeroes. If you don't want the empty string to match, use + instead of *.

Tim Pietzcker
  • 313,408
  • 56
  • 485
  • 544
0

In my case I've used match()

var subject = /^0+$/;
var starting_date = '00000000'
if (starting_date.match(subject)) {
    return true;
}
khashashin
  • 980
  • 11
  • 36
0

I was filtering empty strings of binary and this seemed the most intuitive solution

!+"000" // true

!+"0010" // false