删除<img/>标记内的单词类及其内容

时间:2017-01-17 06:33:49

标签: regex

我想删除单词class及其内容并保留其他标签而不影响段落中的单词:

<p class="Firstpara"><i>Let S be a regular surface, p</i> ∈ <i>S, and <span class="inline"/><img alt="" class="img_Owthpnkbckgrnd_jpg_w23h25_v-10px" src="../Images/img_Owthpnkbckgrnd.jpg" style=""/> = <img alt="" class="img_Owthpnkbckgrnd_jpg_w23h25_v-10px" src="../Images/img_Owthpnkbckgrnd.jpg" style=""/><sub>ϵ</sub></i>(<i>p</i>) <i>a normal neighborhood of p. Then every geodesic segment in <img alt="" class="img_Owthpnkbckgrnd_jpg_w23h25_v-10px" src="../Images/img_Owthpnkbckgrnd.jpg" style=""/> beginning at p is uniquely minimizing.</i></p>

我尝试使用此正则表达式并将其执行到记事本++:

找到:

(<img.*)(class=".*?")(.*?/>)

替换为:

$1$3

但未能保留段落中的字词。

2 个答案:

答案 0 :(得分:0)

你可以试试这个:

(<img[^\/]*?)class="[^"]*"(.*?\/>)

Explanation

&#13;
&#13;
const regex = /(<img[^\/]*?)class="[^"]*"(.*?\/>)/g;
const str = `<p class="Firstpara"><i>Let S be a regular surface, p</i> ∈ <i>S, and <span class="inline"/><img alt="" class="img_Owthpnkbckgrnd_jpg_w23h25_v-10px" src="../Images/img_Owthpnkbckgrnd.jpg" style=""/> = <img alt="" class="img_Owthpnkbckgrnd_jpg_w23h25_v-10px" src="../Images/img_Owthpnkbckgrnd.jpg" style=""/><sub>ϵ</sub></i>(<i>p</i>) <i>a normal neighborhood of p. Then every geodesic segment in <img alt="" class="img_Owthpnkbckgrnd_jpg_w23h25_v-10px" src="../Images/img_Owthpnkbckgrnd.jpg" style=""/> beginning at p is uniquely minimizing.</i></p>`;
const subst = `$1$2`;

const result = str.replace(regex, subst);

console.log(result);
&#13;
&#13;
&#13;

答案 1 :(得分:0)

你可以在notepad ++中试试这个正则表达式:

查找

$1$2

替换:

Edittext

演示:https://regex101.com/r/rUDMeD/5