The provided code represents a simple web application named "Price Extractor." It allows users to input a URL, and upon submission, the application extracts and displays any prices found within the content of the specified URL.
// This function extracts prices from a given URL.
function extractPrices(url) {
// Define a regular expression pattern to match prices in the URL.
const pricePattern = /\$\d+/g;
// Use the pattern to find all matches of prices in the URL.
const matches = url.match(pricePattern);
// If there are matches, convert each match to a price string without the dollar sign.
if (matches) {
const prices = matches.map(match => match.substring(1));
// Join the price strings with a comma separator.
const priceString = prices.join(", ");
// Return the price string.
return priceString;
}
// If there are no matches, return -1 to indicate that no prices were found.
return -1;
}
This code defines a function called extractPrices, which takes a URL as an argument. It uses a regular expression pattern to find all occurrences of a dollar sign followed by one or more digits in the URL. If there are any matches, it extracts the prices by removing the dollar sign and creates a comma-separated string of the prices. If there are no matches, it returns -1.
<!DOCTYPE html>
<html>
<head>
<title>Price Extractor</title>
<link rel="stylesheet" type="text/css" href="styles.css" />
</head>
<body>
<h1>Price Extractor</h1>
<input type="text" id="inputText" placeholder="Enter text with prices" />
<button onclick="priceChecker()">Extract Prices</button>
<p id="priceResult"></p>
<script>
const priceChecker = () => {
const inputText = document.getElementById("inputText").value;
const prices = extractPrices(inputText);
console.log(prices);
};
// This function extracts prices from a given URL.
function extractPrices(url) {
// Define a regular expression pattern to match prices in the URL.
const pricePattern = /\$\d+/g;
// Use the pattern to find all matches of prices in the URL.
const matches = url.match(pricePattern);
// If there are matches, convert each match to a price string without the dollar sign.
if (matches) {
const prices = matches.map((match) => match.substring(1));
// Join the price strings with a comma separator.
const priceString = prices.join(", ");
// Return the price string.
return priceString;
}
// If there are no matches, return -1 to indicate that no prices were found.
return -1;
}
</script>
</body>
</html>
Test case
https://www.amazon.com/product-page$123
Top comments (0)