Мастер-классы по Javascript Екатеринбург Ростов-на-Дону Москва Узнать больше...
Содержание (скрыть) Содержание (показать)

Replace <img..> with <img../>

Replace all occurences of <img...> with <img.../> (add a slash).

var text = '<img src="a">   <img src="b" id="c"/>'

text = text.replace(... your replacement ... )

// now text = '<img src="a"*!*/*/!*>   <img src="b" id="c"/>'

Решение
Решение

An IMG tag with attributes can be represented as <img.*?>.

Now, how can we find an img, ending with /, but not with />? Think about it, maybe you can find a good way. At lease you’ll see which difficulties arise.

Another, easy and lazy way is to match <IMG.? in lazy mode until it ends with either /> or >, and then replace both types of tags with captured <IMG.? plus />.

Here’s the solution:

var text = '<img src="a">   <img src="b" id="c"/>'

text = text.replace( /(<img.*?)\/?>/g, '$1/>' )

alert(text)

#284
Наверх

Реклама

Нашли опечатку?

Нашли опечатку на сайте? Что-то кажется странным?
Выделите соответствующий текст и нажмите Ctrl+Enter!

Последние Комментарии

Помоги другим!

Помоги другим узнать о хорошей статье!