Rasheed kotoor
Rasheed kotoor

Reputation: 329

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

I need to write a JavaScript program where it validates input.

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--(.*)$)

Upvotes: 0

Views: 1089

Answers (2)

Amadan
Amadan

Reputation: 198476

You can use a lookahead assertion for length:

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

From start, at least 50 characters, starting with --NAME--.

Upvotes: 1

Amila Senadheera
Amila Senadheera

Reputation: 13265

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"))

Demo in regex101.com

Upvotes: 2

Related Questions