Javascript Course

The keepFirstImgs() function presented in this page can be used in JavaScript to keep the first Nr IMG tags into a string, and Remove all the other <img>s.
- The function receives the number of the first <img> tags to keep (nrimg), and the string with the HTML text (str); returns the string with the other IMG tags removed.
// Function to keep first $nrimg IMG tags in $str, and removes all the other <img>s
// From: https://coursesweb.net/javascript/
var keepFirstImgs = function(nrimg, str) {
  // gets array with the <img>s that must be stripped from $str ($nrimg+), and removes them
  var remove_imgs = str.match(/(\<img[^\>]+\>)/ig).slice(nrimg);
  var nr_i = remove_imgs.length;
  for(var i=0; i<nr_i; i++) str = str.replace(remove_imgs[i], '');
  return str;
}
- Example:
<script type="text/javascript">
// Function to keep first $nrimg IMG tags in $str, and removes all the other <img>s
// From: https://coursesweb.net/javascript/
var keepFirstImgs = function(nrimg, str) {
  // gets array with the <img>s that must be stripped from $str ($nrimg+), and removes them
  var remove_imgs = str.match(/(\<img[^\>]+\>)/ig).slice(nrimg);
  var nr_i = remove_imgs.length;
  for(var i=0; i<nr_i; i++) str = str.replace(remove_imgs[i], '');
  return str;
}

// Test, keeps the first two IMG tags in $str
var str = 'First img: <img src="img1.jpg" alt="img 1" width="30" />,\
 second image: <img src="img_2.jpg" alt="img 2" width="30">,\
 another Img tag <img src="img3.jpg" alt="img 3" width="30" />,\
 and another image <img src="img_4.jpg" alt="imgage 4" width="30" /> ... etc.';

str = keepFirstImgs(2, str);

alert(str);
/* Result:
 First img: <img src="img1.jpg" alt="img 1" width="30" />,
 second image: <img src="img_2.jpg" alt="img 2" width="30">,
 another Img tag , and another image  ... etc.
*/
</script>

Daily Test with Code Example

HTML
CSS
JavaScript
PHP-MySQL
Which tag adds an image in web page?
<div> <img> <span>
<img src="http://coursesweb.net/imgs/webcourses.gif" width="191" height="63" alt="Courses-Web" />
Which of these CSS codes displays the text oblique?
font-style: italic; text-decoration: underline; font-weight: 500;
#id {
  font-style: italic;
}
Click on the jQuery function used to hide with animation a HTML element.
click() hide() show()
$(document).ready(function() {
  $(".a_class").click(function(){ $(this).hide("slow"); });
});
Click on the correctly defined function in PHP.
fname function() {} function fname() {} function $fname() {};
function fname($a, $b) {
  echo $a * $b;
}
Keep the first Nr IMG tags, Remove all the others

Last accessed pages

  1. Days between two dates, or of a specified week, in PHP MySQL (3632)
  2. Arrays in ActionScript 3 (3081)
  3. sPBM - Simple PHP Backup Manager (3274)
  4. Using server-sent events - EventSource (1497)
  5. JavaScript base64 encode decode (5831)

Popular pages this month

  1. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (324)
  2. Read Excel file data in PHP - PhpExcelReader (118)
  3. The Four Agreements (97)
  4. PHP Unzipper - Extract Zip, Rar Archives (94)
  5. The Mastery of Love (87)
Chat
Chat or leave a message for the other users
Full screenInchide