Replace multiple String quotes

Belgian Malinois 461 Reputation points
2021-04-29T19:02:38.953+00:00

I am trying to replace the following and keep getting an error
"browser":{"enabled_labs_experiments":[],"last_redirect_origin":""
with
"browser":{"enabled_labs_experiments":["same-site-by-default-cookies@2"],"last_redirect_origin":""
I am using
(Get-content "$env:USERPROFILE\AppData\Local\Google\Chrome\User Data\local state") -replace '"browser":{"enabled_labs_experiments":[],"last_redirect_origin":"', '"browser":{"enabled_labs_experiments":["same-site-by-default-cookies@2"],"last_redirect_origin":""'
I am getting
The regular expression pattern "browser":{"enabled_labs_experiments":[],"last_redirect_origin":" is not valid.
At line:1 char:1

Windows for business Windows Server User experience PowerShell
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Anonymous
    2021-04-30T07:25:09.823+00:00

    Hi,

    The "-replace" operator uses regular expressions to find the specified pattern so the escape character "\" needs to be prepended to the brackets.

    (Get-content "$env:USERPROFILE\AppData\Local\Google\Chrome\User Data\local state") -replace '"browser":\{"enabled_labs_experiments":\[],"last_redirect_origin":"', '"browser":{"enabled_labs_experiments":["same-site-by-default-cookies@2"],"last_redirect_origin":""'  
    

    https://learn.microsoft.com/en-us/dotnet/standard/base-types/character-escapes-in-regular-expressions

    Best Regards,
    Ian Xue

    ============================================

    If the Answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.