I need to write a JavaScript program where it validates input.
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@#$%)
- Minimum length of total input (or length of suffix) should be 50 (for example)
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--(.*)$)
CodePudding user response:
Use pattern /^--NAME--.{42,}$/
.{42,}
- This will match 42 or more characters. The total will be 50 including the prefix (--NAME--
).
const regex = /^--NAME--.{42,}$/
console.log(regex.test("--NAME--C$#V"))
console.log(regex.test("--NAME--C$#Vf34F#$f3ftbalc93h34vs#$3gfsddn;yu67u4g3dfvrv34f3f3ff"))
CodePudding user response:
You can use a lookahead assertion for length:
/^(?=.{50})--NAME--.*$/
From start, at least 50 characters, starting with --NAME--
.