使用打字稿从降价中删除一些网址
Remove some urls from markdown with typescript
使用 Typescript 和 Node v8,我正在尝试处理降价文本并删除所有遵循特定模式的图像 URL:
- 包含禁止一词的 URL
- 带有 IP 地址的 URL
我正在尝试使用这个正则表达式:
![([^]]+)]((.(禁止|(?:[0-9]{1,3}.){3}[0-9]{ 1,3}).)\
const markdownText =
'\
* \
*  \
*  \
';
const forbiddenImages = /!\[([^\]]+)\]\((.*(fordbidden|(?:[0-9]{1,3}\.){3}[0-9]{1,3}).*)\)/gm;
function removeForbidden(markdown: string): string {
return markdown.replace(forbiddenImages,"![] ()");
}
console.log(removeForbidden(markdownText)
我只得到输入的第一行:
* ![Allowed image] ()
提前致谢!
在我看来,问题出在字符串中使用的反斜杠。我已经尝试使用 ` 并且它正在工作:
const markdownText =
`
* 
* 
* 
`;
const forbiddenImages = /!\[([^\]]+)\]\((.*(fordbidden|(?:[0-9]{1,3}\.){3}[0-9]{1,3}).*)\)/gm;
function removeForbidden(markdown: string) {
return markdown.replace(forbiddenImages,"![] ()");
}
console.log(removeForbidden(markdownText))
`
使用 Typescript 和 Node v8,我正在尝试处理降价文本并删除所有遵循特定模式的图像 URL:
- 包含禁止一词的 URL
- 带有 IP 地址的 URL
我正在尝试使用这个正则表达式:
![([^]]+)]((.(禁止|(?:[0-9]{1,3}.){3}[0-9]{ 1,3}).)\
const markdownText =
'\
* \
*  \
*  \
';
const forbiddenImages = /!\[([^\]]+)\]\((.*(fordbidden|(?:[0-9]{1,3}\.){3}[0-9]{1,3}).*)\)/gm;
function removeForbidden(markdown: string): string {
return markdown.replace(forbiddenImages,"![] ()");
}
console.log(removeForbidden(markdownText)
我只得到输入的第一行:
* ![Allowed image] ()
提前致谢!
在我看来,问题出在字符串中使用的反斜杠。我已经尝试使用 ` 并且它正在工作:
const markdownText =
`
* 
* 
* 
`;
const forbiddenImages = /!\[([^\]]+)\]\((.*(fordbidden|(?:[0-9]{1,3}\.){3}[0-9]{1,3}).*)\)/gm;
function removeForbidden(markdown: string) {
return markdown.replace(forbiddenImages,"![] ()");
}
console.log(removeForbidden(markdownText))
`