Regular expression matching only 1-digit numbers, and nothing else

Asked

Viewed 280 times

3

I have a variable that can store several values (words, letters, numbers, etc.), depending on an input field. I would like to do a search that relates only 1 digit numbers (/ b d{1} b/), and any other information present in the "false" field as a result. Ex:

var test = "1";
console.log(test.match(/\b\d{1}\b/)); // corresponde e portanto é 'true'
var test = "sp 1 inf"; // mesmo assim corresponde com "test.match(/\b\d{1}\b/)"

What I want is a modification in "test.match(/ b d{1} b/)" that gives "false" as a result in the second case (var test = "1 info"), therefore, not corresponding. Thanks for your attention.

  • In other words, you want regex to marry only if the entire string case, not part of it, is this?

  • By the way, to marry a single digit you don’t need to \d{1}, only \d that’s enough...

2 answers

6


When using match, string looks for a chunk (substring) that matches the regular expression used, wherever it is. As its string contains a single digit isolated, it finds this digit and returns true.

If you want the expression to match the string entire, not only part of it, use ^ at first to match the "beginning of the string" and $ at the end to marry the "end of the string":

^\b\d{1}\b$

Example (also replacing \d{1} for \d, for that is enough to marry a single character):

var test = "1";
log(test.match(/\b\d\b/)); // corresponde e portanto é 'true'
log(test.match(/^\b\d\b$/)); // corresponde e portanto é 'true'

var test = "sp 1 inf";
log(test.match(/\b\d\b/)); // corresponde e portanto é 'true'
log(test.match(/^\b\d\b$/)); // não corresponde e portanto é 'false'


function log(x) {
  document.getElementsByTagName("body")[0].innerHTML += "<p>" + x + "</p>";
}

  • 2

    I was told that to marry a single digit you do not need to \d{1}, only \d that’s enough...

  • 1

    @bfavaretto Hahaha ok, is that I always prefer to stir as little as possible in the AP code to answer the question. Confounding, you know... P

  • I thought your solution was great. // test.match(/ b d b$/) // I had tried before and it hadn’t worked. Now I just figured out why. In some cases there will be a space ( b) before and maybe after the number. The only doubt I have left is how to include this possibility in the proposed solution. Thank you very much for your help.

  • @Eden \b is not a space, but the boundary between two "words". White spaces in general (including tabulations) can be married using \s, and of course, \ (I’m not sure if you need to escape or not, but it doesn’t get in the way...) to just take the common space. Therefore, to detect zero or a space before and after the number you can use ^\ ?\b\d\b\ ?$

  • 1

    @mgibsonbr, Yesterday I even discovered that to include possible spaces, should keep them outside the boundary between words ( b). In my last comment, I mistook s for b, sorry! My solution was almost equal to your s* b d b s*$/, there are just no limits p/ the number of spaces, and it worked perfectly well in all the tests I performed. Well, my doubts end here, I really appreciate your readiness and willingness to help.

1

If you create a capture group for a number with (\d) and you give the flag global g to capture all cases you will have an array con a length corresponding to the number of digits present in the string (or null if there are no numbers in the string).

So if what you want is to know if there is one and only one number you can check if this array that the match returns has only one element, array.length == 1.

function testa(str) {
    var match = str.match(/(\d)/g);
    return !!match && match.length == 1;
}

var a = "1"; 
var b = "sp 1 inf";
var c = "sp 1 inf 5";
var d = "sp inf";

console.log(testa(a)); // true
console.log(testa(b)); // true
console.log(testa(c)); // false (length é 2)
console.log(testa(d)); // false (match é null)

jsFiddle: https://jsfiddle.net/Lj27tzne/

Browser other questions tagged

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