1.0.0 • Published 4 months ago

extract-html-attributes v1.0.0

Weekly downloads
-
License
MIT
Repository
github
Last release
4 months ago

extract-html-attributes

"This NPM package provides a versatile set of functions for extracting attributes from HTML tags 🏷️. The extractAttributeNames function extracts attribute names from HTML tags, excluding tag names and attribute values. On the other hand, the extractAttributes function extracts attributes from HTML tags, including tag names and attribute values. These functions help developers efficiently extract attribute information from HTML strings, enabling them to manipulate and analyze HTML content with ease 🧰. This package is ideal for developers working with HTML content who need to extract specific attribute information for various purposes 🚀."

API

The function extractAttributes Extracts attributes from HTML tags, including tag names and attribute values. and takes one parameter:

  • html (string): The HTML string to extract attributes.

Returns:

  • An array of objects. Each object contains the properties: {tagName, attributeName, valueAttribute} tagName: The tag name of the HTML element. attributeName: The name of the attribute. valueAttribute: The value of the attribute.

The function extractAttributeNames Extracts attribute names from HTML tags, excluding tag names and attribute values. and takes one parameter:

  • html (string): The HTML string to extract attributes.

Returns:

  • An array of attribute names extracted from the HTML string.

Install

$ npm install extract-html-attributes

Examples

Example 1

const { extractAttributes } = require('extract-html-attributes');

const htmlString = `
    <div id="myDiv" class="container" data-custom="value">Hello, World!</div>
    <img src="image.jpg" alt="An image">  
`;

const extractHtml = extractAttributes(htmlString);

The resulting extractHtml array of objects, each representing an attribute extracted from the HTML string htmlString. Each object will have three properties:

tagName: The tag name of the HTML element. attributeName: The name of the attribute. valueAttribute: The value of the attribute.

Example 2

const { extractAttributeNames } = require('extract-html-attributes');

const htmlString = `
    <div id="myDiv" class="container" data-custom="value">Hello, World!</div>
    <img src="image.jpg" alt="An image">  
`;

const extractHtml = extractAttributeNames(htmlString);

The resulting extractHtml array of attribute names extracted from the HTML string htmlString. Each element in the array will be a string representing an attribute name.