JSFiddle - React, Tailwind, and code Playground

by ProstNetpeak

HTML

<a href="http://www.mysite.com/index/yy.jpg">Link to JPG 1</a><BR>
<a href="http://www.mysite.com/index/zz.jpg">Link to JPG 2</a><BR>
<a href="http://www.mysite.com/index/bb.pdf">Link to PDF 1</a><BR>
<a href="http://www.mysite.com/index/aa.pdf">Link to PDF 2</a><BR>
<a href="http://www.mysite.com/products/100.php" target="_blank">New Product: The Super Thing 5000 - $15</a><BR>
<a href="http://www.mysite.com/products/200.php" target="_blank">New Product: The Super Thing X-300 - $20.99</a><BR>

JavaScript

$('a').click(function(){  //assigns a click event to all links on the page
    event.preventDefault() //prevents the link from opening normally
    url = $(this).attr('href') //jQuery gets the url from the href attribute of the link
    linktext = $(this).text() //jQuery gets link text
    if(url.match(/pdf|jpg/i)) {
        alert('Clicked file link');
    } else {
        alert('The usual link is clicked');
        alert("Product Name: " + linktext.match(/New product:(.*) - (\$[0-9]*(\.[0-9]*)?)$/i)[1]); //Displays the text corresponding to the first regular expression  (.*)
        alert("Price: " +linktext.match(/New product:(.*) - (\$[0-9]*(\.[0-9]*)?)$/i)[2]); //Displays the text corresponding to the second regular expression (\$[0-9]*(\.[0-9]*)?)$/i
    }
   
})