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 can be used to create input text field in web page?
<form> <input> <div>
<input type="text" name="a_name" value="val" />
Which CSS property displays the text in a small-caps font?
display font-variant font-style
h3 {
  font-variant: small-caps;
}
What instruction displays a notice box with a message inside it, in JavaScript?
for() Date() alert()
var msg = "Visit CoursesWeb.net";
alert(msg);
Indicate the PHP code used to get the users IP.
$_SERVER["HTTP_USER_AGENT"] $_SERVER["REMOTE_ADDR"] $_GET[]
$ip = $_SERVER["REMOTE_ADDR"];
echo $ip;
Keep the first Nr IMG tags, Remove all the others

Last accessed pages

  1. Star shapes with CSS (10465)
  2. setTimeout and this with bind() method in JavaScript class (4304)
  3. PHP-MySQL free course, online tutorials PHP MySQL code (67646)
  4. Clear Canvas Context (7716)
  5. PHP Predefined Constants (580)

Popular pages this month

  1. PHP Unzipper - Extract Zip, Rar Archives (807)
  2. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (565)
  3. SHA1 Encrypt data in JavaScript (433)
  4. Create simple Website with PHP (398)
  5. Read Excel file data in PHP - PhpExcelReader (392)