1

I hope the form input value not contains any backspace or linefeed char, so I use the Validators.pattern to do, but failed, how this reg should be?

Validators.pattern(/(?![\b|\n])/)
0

3 Answers 3

2

The (?![\b|\n]) pattern presents a negative lookahead that matches a location that is not followed with a backspace, | or a newline. It does not guarantee that the whole string does not match this pattern.

You are looking for

Validators.pattern(/^[^\b\n]+$/)

Or

Validators.pattern("[^\b\n]+")

Note that string patterns are wrapped in ^...$ (="anchored") automatically.

Pattern details

  • ^ - start of string
  • [^ - start of a negated character class
    • \b - a backspace (inside a character class, it is always parsed as a backspace)
    • \n - an LF symbol
  • ]+ - end of the character class, repeat 1+ times
  • $ - end of string.
Sign up to request clarification or add additional context in comments.

Comments

0

What about every character but spaces and line feeds ?

Validators.pattern(^[^\s\n]*$)

Comments

-1

Try putting the argument in quotes : Validators.pattern('/(?![\b|\n])/')

2 Comments

Signature of the method : static pattern(pattern: string | RegExp): ValidatorFn
And if you want to put it in quotes, you need to remove the slashes, as they are defining a regex.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.