尝试使用正则表达式替换 powershell 脚本中的文本

问题描述 投票:0回答:0

我有一个包含降价文本的字符串,我正在尝试将其转换为 html 语法。当我在任何正则表达式测试器中进行测试时,我的代码都很好,但是 powershell 替换运算符不会这样做。我想我错过了什么,但不确定是什么。

有人有什么想法吗?

例子:

$message = "This is a test: [Latest Release Notes](https://learn.microsoft.com/en-us/azure/devops/release-notes/2023/sprint-218-update)"
$markdownLinkRegex = "/\[([^\[\]]+)\]\(([^\(\)]+)\)/g"
# have tried both of these options below
$htmlmessage = $message -replace ([regex]::Escape($markdownLinkRegex)), "<a href='$2'>$1</a>"
$htmlmessage = $message -replace $markdownLinkRegex, "<a href='$2'>$1</a>"

我尝试在正则表达式字符串中添加额外的“\”,但这使情况变得更糟。

期待看到这样的结果:

$htmlmessage = "This is a test: <a href='https://learn.microsoft.com/en-us/azure/devops/release-notes/2023/sprint-218-update'>Latest Release Notes</a>"
regex powershell
© www.soinside.com 2019 - 2024. All rights reserved.