DEV Community

Tahsin Abrar
Tahsin Abrar

Posted on

Price Extractor

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;
}
Enter fullscreen mode Exit fullscreen mode

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>

Enter fullscreen mode Exit fullscreen mode

Test case

https://www.amazon.com/product-page$123
Enter fullscreen mode Exit fullscreen mode

Image of Bright Data

Global Data Access Unlocked – Reach data across borders without restrictions.

Unlock the power of global data collection with our advanced proxy solutions. Ideal for market research and more.

Unlock Data Now

Top comments (0)

Image of Bright Data

Maintain Seamless Data Collection – No more rotating IPs or server bans.

Avoid detection with our dynamic IP solutions. Perfect for continuous data scraping without interruptions.

Avoid Detection

👋 Kindness is contagious

Explore a sea of insights with this enlightening post, highly esteemed within the nurturing DEV Community. Coders of all stripes are invited to participate and contribute to our shared knowledge.

Expressing gratitude with a simple "thank you" can make a big impact. Leave your thanks in the comments!

On DEV, exchanging ideas smooths our way and strengthens our community bonds. Found this useful? A quick note of thanks to the author can mean a lot.

Okay