简体   繁体   English

正则表达式匹配 JavaScript 中以字符串和特定长度为前缀的任何字符

[英]Regex match any characters prefixed with a string and specific length in JavaScript

I need to write a JavaScript program where it validates input.我需要编写一个 JavaScript 程序来验证输入。

Requirement:要求:

  • Input will have a specific prefix.输入将有一个特定的前缀。 (eg: --NAME--) (例如:--姓名--)
  • After this prefix, there can be any characters.在这个前缀之后,可以有任何字符。 (eg: --NAME--any-name_wit#-any*_special_char@#$%) (例如:--NAME--any-name_wit#-any*_special_char@#$%)
  • Minimum length of total input (or length of suffix) should be 50 (for example)总输入的最小长度(或后缀的长度)应为 50(例如)

I was able to write regex for the first two points, but I couldn't include the final point.我能够为前两点编写正则表达式,但我无法包括最后一点。 here is what I have tried for the first two points.这是我为前两点所做的尝试。

input.match(^--NAME--(.*)$)

Use pattern /^--NAME--.{42,}$/使用模式/^--NAME--.{42,}$/

.{42,} - This will match 42 or more characters. .{42,} - 这将匹配 42 个或更多字符。 The total will be 50 including the prefix ( --NAME-- ).总数将为 50,包括前缀 ( --NAME-- )。

 const regex = /^--NAME--.{42,}$/ console.log(regex.test("--NAME--C$#V")) console.log(regex.test("--NAME--C$#Vf34F#$f3ftbalc93h34vs#$3gfsddn;yu67u4g3dfvrv34f3f3ff"))

Demo in regex101.com正则表达式中的演示101.com

You can use a lookahead assertion for length:您可以对长度使用先行断言:

/^(?=.{50})--NAME--.*$/

From start, at least 50 characters, starting with --NAME-- .从头开始,至少 50 个字符,以--NAME--

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

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