It looks like you want to modify the loop so that it skips adding elements to new_products array if match[1] is either "Pris" or "---" (with possible variation in whitespace). Also, given that HTML tags and styles might be present, the comparison should be made with the plain text (i.e., without HTML tags).
Assuming match[1] is where the "pris" would be found and match[2] is where the "källa" would be found, we could adjust the code like this:
javascript
User
also make it check for the: ----------- it changes the amount of --- sometimes
Assistant
If you want to check for a variable number of hyphens in match[1], you can use a regular expression to detect a sequence of hyphens of any length. Here’s how you can adjust the code accordingly:
javascript
while ((match = regex.exec(parsedText)) !== null) {
// Removing the matched text from parsedText
parsedText = parsedText.replace(match[0], '');
// This line appears to be missing its purpose, if you intended to use it for something please clarify
// newparsedText = parsedText.slice(0, match.index);
// Strip HTML tags and trim whitespace from match[1] for comparison
const prisText = match[1].replace(/<[^>]*>?/gm, '').trim();
// Check if prisText is neither "Pris" nor "---"
if (prisText !== "Pris" && prisText !== "---") {
new_products.push({
pris: match[1],
källa: match[2]
});
}
// The above regex might not cover all HTML entities and variations
// You may need to enhance it depending on your input data
}
I've added a simple regex .replace(/<[^>]*>?/gm, '') to strip HTML tags and used .trim() to remove any leading or trailing whitespace. Now, before checking against "Pris" and "---", we have a plain text value without HTML tags.
Note that parsing HTML with regex could lead to issues if the HTML is complex or not well-formed. Using a proper HTML parser would be more reliable if that's the case.
This modification does not store or use newparsedText for anything; it seems that you might have intended for it to be used, but it's unclear from the snippet provided. If newparsedText needs to be used for something else, please clarify its purpose, and we can adjust the code accordingly.
while ((match = regex.exec(parsedText)) !== null) {
// Removing the matched text from parsedText
parsedText = parsedText.replace(match[0], '');
// Strip HTML tags and trim whitespace from match[1] for comparison
const prisText = match[1].replace(/<[^>]*>?/gm, '').trim();
// Regular expression to match a sequence of hyphens/dashes ("-") of any length
const dashRegex = /^-+$/;
// Check if prisText is neither "Pris" nor a sequence of hyphens
if (prisText !== "Pris" && !dashRegex.test(prisText)) {
new_products.push({
pris: match[1],
källa: match[2]
});
}
// The regex used to strip HTML tags might not cover all cases or entities
// You may need to enhance it depending on your specific input text
}
The dashRegex variable holds a RegExp (/^-+$/) that matches a string that is composed exclusively of one or more hyphens (-). If the trimmed prisText is neither "Pris" nor a sequence of hyphens, it will push the product into the new_products array.
Remember, if your parsedText includes special HTML entities or particularly complex HTML, the regex above might not be sufficient to strip the HTML tags properly, and you could consider using a DOM parsing library instead. If you're running this code in a browser environment, you may use the DOMParser API to safely extract text from HTML. If you're running it in a Node.js environment, you might use a library like jsdom or cheerio. Similarly, for the dashes, consider the broader context and ensure that this doesn't accidentally match text you want to keep.