user5765012
user5765012

Reputation:

Javascript regex for string followed by exact number of chars

I am looking for a regex for matching the following ids:

path-0
path-1
path-2

And does not match ids with a say bigger number of characters, eg:

path-0-0
path-0-1

Currently I have this chunk of code:

let array = [
  'path-0', 'path-1', 'path-2', 'path-0-0', 'path-0-1'
];

let reg = /[^path-]{6}/;
    
$.each( array, function(index, value) {
  if(reg.test(value) ) {
    console.log(reg.test(value), value);  
  } else {
    console.error(reg.test(value), value);
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

Upvotes: 0

Views: 371

Answers (3)

ctwheels
ctwheels

Reputation: 22817

Brief

As per my original comments...

The reason your pattern [^path-] is not working is because it matches anything not present in the set, so any character except ahpt- (I rearranged the characters so you can better understand what it's saying). Character sets are used when you want to say match anything that is (or is not) in this set.

An example of using a set, for your example, would be ^path-[0-9]$. This says *match a character between 0 and 9 in the ASCII table, which includes 0123456789. Another way of defining the same character set is using the shorthand token \d (which specifies the same characters 0 through 9).

The caret symbol ^ used at the start of a set specifies that the set should negate these characters. An example of its use for your example is ^path-[^\D]$, which is basically a double-negative specifying anything that is not not a digit (or simply put any digit). Anywhere outside a set (and assuming it's not escaped) ^ means assert position at the start of the string.

Code

^path-\d$

let array = [
  'path-0', 'path-1', 'path-2', 'path-0-0', 'path-0-1', 'path-12'
];

let reg = /^path-\d$/;
    
$.each( array, function(index, value) {
  if(reg.test(value) ) {
    console.log(reg.test(value), value);  
  } else {
    console.error(reg.test(value), value);
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

If you want to match the possibility of more than 1 digit, you can use the following.

^path-\d+$

let array = [
  'path-0', 'path-1', 'path-2', 'path-0-0', 'path-0-1', 'path-12'
];

let reg = /^path-\d+$/;
    
$.each( array, function(index, value) {
  if(reg.test(value) ) {
    console.log(reg.test(value), value);  
  } else {
    console.error(reg.test(value), value);
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>


Explanation

  • ^ Assert position at the start of the string
  • path- Match this literally
  • \d Match any digit
  • $ Assert position at the end of the string

Upvotes: 0

Federico klez Culloca
Federico klez Culloca

Reputation: 27119

First of all, you want to match beginning of string

^

Then the word "path" followed by a dash

^path-

Then a single character and nothing more (so, end of line)

^path-\d$

As per Arthur's comment, if your intention is to allow ids with more than one digit after "path-" you can specify that you want 1 or more digits by suffixing \d with +, like this:

^path-\d+$

Upvotes: 1

Canta
Canta

Reputation: 1480

What you are looking for is a way to check if pattern /-\d+/ only occurs once. Notice that the symbol + here works to match numbers with more than one digit. You need though to check for string end, represented by $ symbol, the same way ^ represents the beginning of it. The best regex option that will work for you would be:

let reg = /^path-\d+$/;

Upvotes: 0

Related Questions