2017-10-12 97 views
0

我想如何通過JavaScript和其他文本

<images src="vendor/chessboard.js/img/chesspieces/wikipedia/wR.png" alt="" class="piece-417db" data-piece="wR" style="width: 67px;height: 67px;"></images> 

這個文本轉換成

<img src="vendor/chessboard.js/img/chesspieces/wikipedia/wR.png" alt="" class="piece-417db" data-piece="wR" style="width: 67px;height: 67px;" /> 

我怎麼可以這樣做替換文本???

+0

htt ps://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace –

+0

我想知道什麼是正則表達式。 –

+0

那你究竟嘗試了什麼? –

回答

0

您可以使用網站like this one拿出正則表達式的味道你是後

//Switch <images to <img 
var pattern = /<images/gi;//declare your regex pattern 
var originString = '<images src="asdf">';//the string to go against 
console.log(originString);//show the string as is before modding it 
console.log(originString.replace(pattern, "<img"));//show the modded string 

//remove </images> 
var pattern2 = /<\/images>/gi; 
var originString2 = '<img src="blahblah"></images>'; 
console.log(originString2); 
console.log(originString2.replace(pattern2, "")); 
0

使用String.replace改造<images></images>出現組成<img/>分別爲:

const text = '<images src="vendor/chessboard.js/img/chesspieces/wikipedia/wR.png" alt="" class="piece-417db" data-piece="wR" style="width: 67px;height: 67px;"></images>'; 
 

 
const result = text.replace(/<images/gi, '<img').replace(/><\/images>/gi, '/>'); 
 

 
console.log(result); // -> <img ... />

相關問題