2017-02-20 159 views
-2

這裏是在Mardown文本..的Javascript替換字符串

![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> 

在上述字符串需要用空使用JavaScript(替換)取代

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

正則表達式..

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

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

這裏的文字是Mardowntext,鏈接是需要被替換的字符串..

+0

能否請您分享您的努力? – Rajesh

+0

對於正則表達式'const regex = //g;'? – getjackx

+0

@getjackx感謝您的重播,但降價字符串可能包含多個視頻,因此需要標識特定的視頻並用空白替換。 –

回答

1

也許是這樣的:

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

應該配合他們的內部唯一來源標籤的所有可能的視頻標籤。

regexr.com是測試這種正則表達式的好地方。

0

您可以嘗試使用indexOf()來獲取視頻標籤的索引,然後嘗試替換。 示例發現:

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

replacing with index position