-1

I have requirement to restrict a string using regex in angular. I used ^(?!.Test).$ but the issue is that it restrict all the string starting with word Test i.e if i type Test1 , testimony it fails. Validation shall only restrict Test word. It shall work as :

1) Test - not allowed

2) test- not allowed

3) Test123 - Allowed

4) Testimony - Allowed

How do we do it regex.

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
Ankit
  • 25
  • 3

1 Answers1

-1

You need word boundary

let test1 = "Test";
let test2 = "test";
let test3 = "Testsdfsdf";

let regex = /^Test\b/i;

console.log(regex.test(test1));
console.log(regex.test(test2));
console.log(regex.test(test3));
Aftab Khan
  • 3,853
  • 1
  • 21
  • 30