Javascript 替换字符串

Javascript Replace string

这是 Mardown 文本..

![sample text](/api/article/58933542dbac1da7eba747e0/file/58933542dbac1da7eba747e1/binary/A3DD9420-C5BA-426A-A51A-296C263440FF.png).fgfgfdgdfgdfgfg fg.<video controls poster='tempfile://827e9ff4-07ef-f9c1-a4c2-5d4f8fcb5754'><source src='tempfile://d23228e7-a683-befc-724b-96f57f5459e7' type='video/mp4'></video>

上面的字符串需要替换

 <video controls poster='tempfile://827e9ff4-07ef-f9c1-a4c2-5d4f8fcb5754'><source src='tempfile://d23228e7-a683-befc-724b-96f57f5459e7' type='video/mp4'></video>

使用 javascript(替换)正则表达式为空..

 function RemoveContentText(text, link) {
    return text.replace(/(!\[.*?\]\()(.+?)(\))/g, function (whole, a, b, c)  {

        if (whole == link) return '';
        else return whole;
    });
}

这里的text是Mardowntext,link是需要替换为空的字符串..

可能是这样的:

/<\s*video[^>]*>\s*(?:<\s*source[^>]*>\s*)*<\s*\/\s*video\s*>/ig

它应该匹配所有可能的视频标签,其中只有源标签。

regexr.com 是测试此类正则表达式的好地方。

您可以尝试使用indexOf() 获取video 标签的索引,然后尝试替换。 查找示例:

var str = "Hello world, welcome to the universe."; var n = str.indexOf("welcome"); 答案:13

replacing with index position