使用typescript从markdown中删除一些url



使用Typescript和Node v8,我试图处理一个标记文本,并删除所有遵循特定模式的图像URL:

  • 包含单词"禁止"的URL
  • 带有IP地址的URL

我正在尝试使用以下正则表达式:

[([^]+(]((.(fordbidden|(?:[0-9]{1,3}.({3}[0-9]{1,3}(.\

这个打字脚本代码:

const markdownText =
'
* ![Allowed image](http://accepteddomain/image.png)
* ![Forbidden domain image](http://fordbidden/image.png) 
* ![Forbiden IP image](http://80.11.20.15/image.png) 
';
const forbiddenImages = /![([^]]+)]((.*(fordbidden|(?:[0-9]{1,3}.){3}[0-9]{1,3}).*))/gm;
function removeForbidden(markdown: string): string {
return  markdown.replace(forbiddenImages,"![$1] ()");
}
console.log(removeForbidden(markdownText)

我只得到输入的第一行:

* ![Allowed image] ()           

提前感谢!

在我看来,这个问题来自于字符串中使用的反斜杠。我试过使用`并且它正在工作:

const markdownText =
`
* ![Allowed image](http://accepteddomain/image.png)
* ![Forbidden domain image](http://fordbidden/image.png) 
* ![Forbiden IP image](http://80.11.20.15/image.png)
`;
const forbiddenImages = /![([^]]+)]((.*(fordbidden|(?:[0-9]{1,3}.){3}[0-9]{1,3}).*))/gm;
function removeForbidden(markdown: string) {
return  markdown.replace(forbiddenImages,"![$1] ()");
}
console.log(removeForbidden(markdownText))

`

最新更新