[英]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:要求:
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"))
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.