简体   繁体   中英

Using the replace operator on a string that has quotes powershell

I am looking to run the command

foreach-object {$_ -replace

However the string I am attempting to work with could be described as the follow

this string "has" quotes

the whole line being

foreach-object {$_ -replace "this string "has" quotes", "this string "won't have" quotes"}

How do I make this quote filled line work with powershell?

You can either escape the nested double quotes like so `" or better yet, use single quotes for quoting of the string then you won't need to escape the double quotes eg:

'this string "has" quotes'

Note: with single quotes you won't get variable expansion in a string.

Did you try doubling up you quotation marks as an escape character for quotes? ie

$a = "This String ""Has"" Quotes"
$a = $a.replace("This String ""Has"" Quotes","this string ""won't have"" quotes. ")

You can Grave (the tilde key) double quotation marks as an escape character. You can do this to any special character in a string.

eg

$a = "This String `"Has`" Quotes"
$a = $a.replace("This String `"Has`" Quotes","this string won't have quotes. ")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM