简体   繁体   English

字符串的Javascript正则表达式,后跟确切的字符数

[英]Javascript regex for string followed by exact number of chars

I am looking for a regex for matching the following ids: 我正在寻找用于匹配以下ID的正则表达式:

path-0
path-1
path-2

And does not match ids with a say bigger number of characters, eg: 并且与ID的字符数匹配,例如:

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> 

First of all, you want to match beginning of string 首先,您要匹配字符串的开头

^

Then the word "path" followed by a dash 然后,单词“ path”后跟一个破折号

^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: 根据亚瑟(Arthur)的评论,如果您打算允许ID在“ path-”之后使用多于一位的数字,则可以通过在\\d后缀+来指定想要1位或更多的数字,如下所示:

^path-\d+$

What you are looking for is a way to check if pattern /-\\d+/ only occurs once. 您正在寻找一种检查模式/-\\d+/仅发生一次的方法。 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+$/;

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). 您的模式[^path-]不起作用的原因是因为它匹配集合中不存在的任何内容,因此除了ahpt-以外的任何字符(我都重新排列了字符,以便您可以更好地理解它的意思)。 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]$ . 例如,使用集合的一个示例是^path-[0-9]$ This says *match a character between 0 and 9 in the ASCII table, which includes 0123456789 . 这表示*匹配ASCII表中09之间的字符,其中包括0123456789 Another way of defining the same character set is using the shorthand token \\d (which specifies the same characters 0 through 9 ). 定义相同字符集的另一种方法是使用速记标记\\d (指定相同的字符09 )。

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). 在您的示例中使用它的一个示例是^path-[^\\D]$ ,它基本上是一个双负数,它指定不是数字(或简单地放置任何数字)的任何内容。 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. 如果要匹配大于1位的可能性,可以使用以下内容。

^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 path-从字面上匹配
  • \\d Match any digit \\d匹配任何数字
  • $ Assert position at the end of the string $在字符串末尾声明位置

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM