Regex for textarea

Viewed 288

I am trying to build a regex for textarea in my angular 8 application. The textarea should allow all characters but should not allow empty character at the start. I have used 3 but both have the following problems:

  1. ^(?!\s*$).+ -- This does not work if user goes to next line in the textarea using enter key.
  2. ^[^\s]+(\s+[^\s]+)*$ -- This does not work if there is an empty space between 2 words.
  3. ^(?!\s*$)[-a-zA-Z0-9' '.,\(\)&+\/]{1,}$ -- This does not work if user goes to next line in the textarea using enter key.

Can anyone help me with this?

1 Answers

You can use

/^\S.*(?:\r?\n\S.*)*$/u

See the regex demo.

Details

  • ^ - start of string
  • \S.* - a non-whitespace and the rest of a line
  • (?:\r?\n\S.*)* - zero or more repetitions of
    • \r?\n - a CRLF or LF line break sequence
    • \S.* - a non-whitespace and the rest of a line
  • $ - end of string.
Related