Html suppress certain tags when extracting

A text replace node would be amazing

it’s def not stripping the content out, what other options do you have in mind that I can use?

i think the issue is with the regex A | regex B

using just one part of it seems to work.

So the pipe | doesn’t seem to be taken into account

EDIT: actually i think it only stripe out the first finding, not all of them from the content

ok, so THIS DOESNT WORK, but you get the idea, can it not be split into 2 if statements to cover both the regex?

const regex = /<img .*?>/gm;
const regex2 =/<a .*?<\/a><br>/gm;
const response = [];


for (const item of items) {
  let content = item.json.Content;
  const matched = content.match(regex);
  if (Array.isArray(matched)) {
    for (const match of matched) {
      content = content.replace(match, 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx')
    }
  }
   
}

for (const item of items) {
  let content = item.json.Content;
  const matched2 = content.match(regex2);
  if (Array.isArray(matched2)) {
    for (const match of matched2) {
      content = content.replace(match, 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa')
    }
  }
  
  response.push({
    json: {
     content,
     }
  })
}

return response;

It does not work because you are using two loops. It is best to use only one loop for this. (In each loop you read the data again: let content = item.json.Content;)
Also, the data is written to the response only in the second loop.
Just try to put content = content.replace(match, 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx') into the second loop, using content = content.replace(match, 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa')

1 Like

cheers will have a go.

javascript i’ve only started learning in the last few months since using n8n, making progress but not enought yet :wink:

EDIT: yeah, still unable to get it to work, but i think this is possibly the only way i will get it to work, but splitting the regex and replace into 2 separate calls.

ok this seems to have fixed it, it didnt like the regex

const regex2 = /<img .*?>/gm;
const regex = /<a[^>]*>(.*?)<\/a>/gm;


const response = [];

for (const item of items) {
  let content = item.json.Content;
  const matched = content.match(regex);
  const matched2 = content.match(regex2);
  if (Array.isArray(matched)) {
    for (const match of matched) {
      content = content.replace(match, 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx')
      if (Array.isArray(matched2)) {
    for (const match2 of matched2) {
            content = content.replace(match2, 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa')
    }
    }
  }
  
  response.push({
    json: {
     content,
     }
  })
}}

return response;

I’m glad you fix it and hope you learn a little bit :slight_smile:
Now i also clean up your code, because you don’t need to match if you just want to replace some stuff with regex:

const regex = /<img .*?>/gm;
const regex2 =/<a .*?<\/a><br>/gm;

const response = [];

for (const item of items) {
  let content = item.json.Content;
  content = content.replace(regex, 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx')
  content = content.replace(regex2, 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa')
  
  response.push({
    json: {
     content,
     }
  })
}

return response;
3 Likes