Check if a string is only composed of 0

Asked

Viewed 320 times

8

I want to check if for example a string "00000000" is composed only of zeros and give true, but a "0000a0e0b" is false, because I tried with ! isNaN and gives true having at least 1 number

var palavra = 00000;
var palavra2 = a00a0;
if(!isNaN(palavra)) // Aqui é pra dar true
if(!isNaN(palavra2)) // Aqui era pra dar false, mas dá true

4 answers

7


If you are testing strings, an alternative is to use regular expressions:

var palavra = "00000";
var palavra2 = "a00a0";

console.log(/^0+$/.test(palavra)); // true
console.log(/^0+$/.test(palavra2)); // false

  • ^ indicates the start of the string
  • 0+ indicates one or more occurrences of 0
  • $ indicates the end of the string

That is, the expression /^0+$/ corresponds to a string with one or more occurrences of 0, from start to finish.

The method test checks if the string passed matches the expression.

  • I removed my own. ;)

  • 1

    No problem, your answer was great.

  • @dvd I had thought of removing mine too. I had never happened this before and I was in doubt about what to do.. Anyway, thanks!

  • 1

    In these cases who answered last that should remove the answer. But as it was practically at the same time, I took the initiative. No problem no. Good thing you warned because I had not even seen your answer rs.

  • 1

    many thanks to all who answered, it worked here :)

4

As my previous reply was identical to @hkotsubo and we posted exactly at the same time, I will leave a variant for reference.

You can also use a negation regular expression:

[^0]

The sign ^ (negated set or "negated set") will check whether the string has any other character than 0. Finding yourself is because you don’t just have 0. To reverse the check, you can use the signal !. In the case, true will indicate that you only have 0.

Example:

var palavra = "00000000";
var palavra2 = "0000a0e0b";

console.log(!/[^0]/.test(palavra)); // retorna true, só contém 0
console.log(!/[^0]/.test(palavra2)); // retorna false, não tem só 0 ou nem tem 0

  • 1

    Very good, I had not thought of this alternative. As I had already voted before, I keep my vote! ;)

  • 2

    I like this alternative, I will try to use it in a next c;

  • 1

    @Paulohenriquerodriguesgrund Cool. I just put to show that one can do it in different ways.

4

I would force a conversion to number with the operator +, then would check if gave 0. It’s quite simple:

var palavra = '00000';
var palavra2 = 'a00a0';
console.log(+palavra === 0);
console.log(+palavra2 === 0);

Only one drawback: if the string is 0b00000 (with any amount of zeros after the b), can be interpreted as binary string of value 0 (support depends browser), and the result of the comparison will give true.

3

Since you already have answers with regular expressions, I show you an alternative without regular expressions, although it is not so compact.

The idea is to test with another string built with zeros, for the same amount of letters, at the expense of the method repeat string. This repeat will repeat a zero several times until it has the corresponding size.

Example:

var palavra = "00000000";
var palavra2 = "0000a0e0b";

console.log(palavra === "0".repeat(palavra.length));
console.log(palavra2 === "0".repeat(palavra2.length)); 

Browser other questions tagged

You are not signed in. Login or sign up in order to post.