Aymand Osanes
Aymand Osanes

Reputation: 797

JS - Check if string contain only 0

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.

Upvotes: 5

Views: 11524

Answers (3)

Jonatan
Jonatan

Reputation: 11

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

!+"000" // true

!+"0010" // false

Upvotes: 1

khashashin
khashashin

Reputation: 1137

In my case I've used match()

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

Upvotes: 1

Tim Pietzcker
Tim Pietzcker

Reputation: 336158

/^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 *.

Upvotes: 16

Related Questions