I am new to regular expressions. I have a requirement to replace spaces in a piece of multi-line text. Replacement Rules:
- Replace all spaces at the beginning of the line with non-breaking space (
). - Replace any instance of repeating spaces (more than one space together) with the same number of non-breaking spaces.
- Individual spaces that are not at the beginning of a line remain untouched.
I used Regex Coach to create the appropriate template:
/( ){2,}|^( )/
Suppose I have this input:
asdasd asdasd asdas1
asda234 4545 54
34545 345 34534
34 345
Using the PHP regular expression replacement function (e.g. preg_replace()), I want to get this output:
asdasd asdasd  asdas1
 asda234 4545    54
  34545 345  34534
34 345
, , , , .