是否存在针对特殊字符的 PowerShell 转义功能

2023-12-07

As with in the self-answer, I like to be able to remove control characters from text to get a single line string.
Does there exists a PowerShell Escape function for Special PowerShell Characters?
(Similar to the Regex.Escape(String) Method for escaping special regex characters)
Or is there a smarter way to do this than the self-answer?
(I am less concerned with the Unicode escape sequence: `u{x})


Using a 正则表达式回调:

$Backtick = @{}
foreach ($Char in '0abefnrtv`'.ToCharArray()) {
    $Backtick[$ExecutionContext.InvokeCommand.ExpandString("``$Char")] = "``$Char"
}
$SpecialCharacters = '[' + (-Join $Backtick.get_Keys()) + ']'
function Escape($Text) {
    [regex]::Replace($Text, $SpecialCharacters, { param($Match) $Backtick[$Match.Value] })
}
Escape 'Test
123'

Test`n123
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

是否存在针对特殊字符的 PowerShell 转义功能 的相关文章

随机推荐