Multiline in JavaScript regexes

Regexes when working with multiline strings have their own features. Let's deal with them using an example of the following string usual characters, newlines and tabs characters:

javascriptlet str = `111
  222
  333
  444`;

Newline

Newline characters can be caught with the '\n' command:

javascriptlet res = str.replace(/\n/g, '!');

As a result, the following will be written to the variable (the spaces between the lines are tabulation):

javascript`111!  222!  333!  444`;

Tabulation

Tab characters can be caught with the '\t' command:

javascriptlet res = str.replace(/\t/g, '!');

As a result, the following will be written to the variable:

javascript`
                111
                !222
                !333
                !444
            `;

Dot character

The '.' command for a multiline string doesn't catch newline characters:

javascriptlet res = str.replace(/./g, '!');

As a result, the following will be written to the variable:

javascript`
                !!!
                !!!!
                !!!!
                !!!!
            `;

Any character

To catch all the characters in a multiline string, a tricky trick is used in the form of a [\s\S] combination. This construction will find all usual characters and all newlines:

javascriptlet res = str.replace(/[\s\S]/g, '!');

As a result, the following will be written to the variable:

javascript`!!!!!!!!!!!!!!!!!!`;

Hat

With the 'm' modifier you can enable multiline mode. In this case, the hat will catch the beginning of each line:

javascriptlet res = str.replace(/^/gm, '!');

As a result, the following will be written to the variable:

javascript`
                !111
                !222
                !333
                !444
            `;

Dollar

The '$' command in multiline mode will catch the end of each line:

javascriptlet res = str.replace(/$/gm, '!');

As a result, the following will be written to the variable:

javascript`
                111!
                222!
                333!
                444!
            `;

Practical tasks

Задание 1

Given a string:

javascript`
  abc
  def
  ghi
  jkl
`;

Write a regex that will make this string the following:

javascript`
  abc!
  def!
  ghi!
  jkl!
`;
Задание 2

Given a string:

javascript`
  abc
  def
  ghi
  jkl
`;

Write a regex that will make this string the following:

javascript`
!  abc
!  def
!  ghi
!  jkl
`;
Задание 3

Given a string:

javascript`
  abc
  def
  ghi
  jkl
`;

Write a regex that will make this string the following:

javascript`!
  abc
  def
  ghi
  jkl
!`;
Задание 4

Given a string:

javascript`
  abc
  def
  ghi
  jkl
`;

Write a regex that will make this string the following:

javascript`!
!  abc
!  def
!  ghi
!  jkl
!`;
Задание 5

Given a string:

javascript`
  abc
  def
  ghi
  jkl
`;

Write a regex that will make this string the following:

javascript`!
  abc!
  def!
  ghi!
  jkl!
!`;
Задание 6

Given a string:

javascript`
  abc
  def
  ghi
  jkl
`;

Write a regex that will make this string the following:

javascript`
                    !abc
                    !def
                    !ghi
                    !jkl
`;