I’m trying to remove an H1 Element from HTML. I thought I may be able to do this by using the Text Helper and replace the regex of <h1>.*<\/h1> with an empty string but I don’t know how to enter an empty string in the Replace Value. so this actually leads me to 2 questions:
Is there a better way to remove an element from HTML?
In case I simply want to remove some text from another text, how can I do this? Generally the equivalent of str.replace("my text", "") in JS.
I’ve been trying to do it with the Text Helper as well and, but I couldn’t find a way to set the replace value to an empty string.
I prefer using the code block to handle this task myself. The following code snippet should do the trick. Currently it simply removes the <h1> tags, but you might want to replace them with <p> tags depending on what you need for your output HTML.
Here’s the code to remove the <h1> tags:
// Define the entry point function as 'code'
export const code = async (params: any) => {
// Function to remove <h1> and </h1> tags from a text
function removeH1Tags(text: string): string {
const regex = /<\/?h1>/gi;
return text.replace(regex, '');
}
// Assuming the text to process is passed as a parameter named 'inputText'
const inputText = params.inputText || '';
// Process the text
const outputText = removeH1Tags(inputText);
// Return the processed text directly
return outputText;
};
Would it also be possible to use a single Text Helper step to replace multiple distinct values within a single step?
For instance, in the screenshot provided, the regex is designed to match both <h1> and </h1>. However, it only replace the opening <h1> tag and not the closing tag.
I appreciate the quick fix on this but it is not perfect, for a few reasons:
The response removes the <h1> element, but keep a whitespace/empty row there. my expectation is to have that element removed and nothing else appearing instead of it. for example if I have the following
<h1>my h1 text</h1>
<h2>my h2 text</h2>
The response will be
<h2>my h2 text</h2>
Whereas I’m expecting
<h2>my h2 text</h2>
Another thing is with the implementation of replace all instances. I feel like this should be an option on the operation and not apply to all automatically. there may instances where i’d like to remove one instance of <H1> but not all.