How can I remove www.example.ru from src ?

<textarea id="content" cols="60" rows="10"> <div id="description"><div style="text-align: center;"> <img alt="Стенд и картинки для уголка безопасности" border="0" src="www.example.ru/images/goods/umey_deystvovat.jpg" title="Стенд и картинки для уголка безопасности"> <img alt="Пожарная безопасность в школе" border="0" src="www.example.ru/images/goods/pb_foto_2.jpg" title="Пожарная безопасность в школе"> </div> <img src="cat.jpg"> </textarea> 
  • through id using jquery I can do it, but how to change right away is not enough for everyone. - votanko
  • as well as via id, just use class or tag for sampling - Grundy
  • Why do you have a <textarea> open and not close? Do you want to change textarea inside? - Sergiks
  • One </div> is missing - Mr. Black

5 answers 5

you can add id = "needtodelete" on the element and will be like this

 <textarea id="content" cols="60" rows="10"> <div id="description"><div style="text-align: center;"> <img id="needtodelete" alt="Стенд и картинки для уголка безопасности" border="0" src="www.example.ru/images/goods/umey_deystvovat.jpg" title="Стенд и картинки для уголка безопасности" /> <img alt="Пожарная безопасность в школе" border="0" src="www.example.ru/images/goods/pb_foto_2.jpg" title="Пожарная безопасность в школе" /> </div> 

and then in the script:

 var a=document.getElementById("needtodelete"); a.src=a.src.replace(/www.example.ru/g,""); 

or else you can delete before the first occurrence of the character '/'

  • chrome writes "Cannot read property 'src' of null" - votanko
  • probably you have a mistake in the name id and in the variable a goes null ... - GetYouFun
  • You were right, but src has not changed. www.example.ru/images/goods/umey_deystvovat.jpg - votanko
  • @votanko works everything out for me, the textarea may be in the code, it doesn’t find the picture with me, because it becomes text - GetYouFun
  • var test = $ ('img'). attr ('src'). replace ('www.example.ru/', '/'); $ ('img'). attr ('src', test); I did it, it worked, but all the pictures became the same. Is there any way to modify this code? - votanko

you wrote

through id using jquery I can do it, but how to change right away is not enough for everyone.

you can add a class "classname123" for each such element and then

 var a=document.getElementsByTagName("img"); for(i=0;i<a.length;i++) { if (a[i].className=="classname123") { a[i].src=... } } 

    For example, on jQuery:

     function parseUrl( url ) { var a = document.createElement('a'); a.href = url; return a; } $("img[src]").each(function () { var link = parseUrl($(this).attr("src")); var output = link.pathname + link.search + link.hash; $(this).attr("src", output); }) 

    Jsfiddle

      Simply

       textarea = document.querySelector('textarea'); textarea.innerHTML = textarea.innerHTML.replace(/(http|www).*ru\//ig, ''); 
       <textarea id="content" cols="60" rows="10"> <div id="description"><div style="text-align: center;"> <img alt="Стенд и картинки для уголка безопасности" border="0" src="https://www.example.ru/images/goods/umey_deystvovat.jpg" title="Стенд и картинки для уголка безопасности"> <img alt="Пожарная безопасность в школе" border="0" src="www.example.ru/images/goods/pb_foto_2.jpg" title="Пожарная безопасность в школе"> </div> <img src="cat.jpg"> </textarea> 

        For all <img> tags in a document, in the src attribute, the substring can be changed like this:

         Array .prototype .forEach // спасибо @Grundy .call( document.getElementsByTagName('img'), function(e) { e.src = e.src.replace(/(https?:\/\/)?www.example.ru\/?/ig, ''); } ) ; 
         <div id="description"> <div style="text-align: center;"> <img alt="Стенд и картинки для уголка безопасности" border="0" src="www.example.ru/images/goods/umey_deystvovat.jpg" title="Стенд и картинки для уголка безопасности" /> <img alt="Пожарная безопасность в школе" border="0" src="www.example.ru/images/goods/pb_foto_2.jpg" title="Пожарная безопасность в школе" /> </div> <img src="cat.jpg"> 

        In your question, the replacement code is inside the textarea tag, which makes all this html just plain text. Then you need to change the regular form, which includes src= , and, optionally, check that the string "src" is inside the <img> . Without unnecessary checks:

         var el = document.getElementById("content"); el.innerHTML = el .innerHTML .replace(/(src=")www.example.ru/ig, "$1") ; 
         <textarea id="content" cols="60" rows="10"> <div id="description"><div style="text-align: center;"> <img alt="Стенд и картинки для уголка безопасности" border="0" src="www.example.ru/images/goods/umey_deystvovat.jpg" title="Стенд и картинки для уголка безопасности"> <img alt="Пожарная безопасность в школе" border="0" src="www.example.ru/images/goods/pb_foto_2.jpg" title="Пожарная безопасность в школе"> </div> <img src="cat.jpg"> </textarea> 

        • Odd me does not work. Most likely because elements cannot be found in textarea . Everything inside is text - Mr. Black
        • it’s better forEach because you need to change the elements, rather than get a new collection - Grundy
        • @Grundy is not better, because ES5 may not be supported by some browsers, unlike .map() . The elements change beautifully . - Sergiks
        • @Sergiks, map and forEach functions from one set, if one works - the other works, the main difference is that forEach does not create a new array. unlike map - Grundy
        • Similarly, .map() , also from ES5: (I thought it was ancient. My bad. - Sergiks