I want to replace \n
with \\n
, and \a
with \\a
and so on...
So this is my regex:
Pattern:
([\\])
Should be replaced with:
$1$1
So if the input string is \n
, it will be replace with \n
.
(I have tested it at http://www.regexe.com/ and it worked)
Then I had this PHP code:
preg_replace('/([\\])/', '$1$1', "\n a");
and thought it would output \n a, but instead I got this error:
Warning: preg_replace(): Compilation failed: missing terminating ] for character class at offset 5
So I have escaped the [
.
Now my code is
preg_replace('/(\[\\])/','$1$1',"\n a");
But.... this doesn't works as I want, because now it is not replacing \n
with \\n
.
What am I doing wrong?