简体   繁体   English

在带引号powershell的字符串上使用replace运算符

[英]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? 如何使这个引用填充行与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. 您可以将Grave(波浪号)双引号用作转义字符。 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. ")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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