I am trying to replace a string in a file like this:
c:\>powershell -Command (gc D:\xxxxxxx\index.html) ^
-replace '^<script src="js/locale/languageholder.js"^>^</script^>', '' ^
| Out-File -encoding ASCII "D:\yyyyyyyyy\index.html"
the string I want to remove is:
<script src="js/locale/languageholder.js"></script>
no errors, but it is just not replaced. I assume there must be some charactors to be escaped, but couldn't figure it out.
In the code ^ is used to escape <, otherwise it will show this error:
< was unexpected at this time.
Getting the same error using \ to escape
-replaceis a regex operator and^is a regex metacharacter describing the starting position of a string. Since no string starts in multiple places, it doesn't work. If you're expecting to find a literal caret^you need to escape it with a backslash, eg.\^<script src=...^is not an escape sequence in neither regex nor PowerShell. Are you running this from a batch script? Or interactively fromcmd.exe?