Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

222
Views
Is there any way to remove the "Code Snippet" from a string?

Let's say I have figure tags in a string

const content = 'Here is a sentence followed up with <figure><img /></figure> plus some more text.'

Is there a way I can check to see if there is a figure tag in the string, select the tags and all of its contents, and remove or filter out all of them with React or Javascript?

The desired output would be

const content = 'Here is a sentence followed up with plus some more text.'

I know it is not clean but it supports an Algolia query.

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

You can use RegEx for this. One possible RegEx would be /(<figure>.+<\/figure>)/g (https://regexr.com/6ke0o)

So some sample code would look like this:

const content = 'Here is a sentence followed up with <figure><img /></figure> plus some more text.'.replace(/(<figure>.+<\/figure>)/g, '')
about 4 years ago · Juan Pablo Isaza Report

0

if you only want to remove tags (NOT tag body):

console.log("Here is a sentence followed up with <figure><img /><\figure> plus some more text.".replaceAll(/<\\?[a-zA-Z\s]+\/?>/g,""))

or:

.replaceAll( /(<([^>]+)>)/ig, '');

if you want to just remove figure tag:

console.log("Here is a sentence followed up with <figure><img/><\figure> plus some more text.".replaceAll(/<figure>.+<\figure>/g,""))

about 4 years ago · Juan Pablo Isaza Report

0

You could use the DOM parser, create an element, put your content in as innerHTML, and filter the childNodes so that only text nodes remain.

const content = 'Here is a sentence followed up with <figure><img /></figure> plus some more text.'

const tester = document.createElement('div');
tester.innerHTML = content;

const output = [...tester.childNodes].filter(child => child.nodeType === 3).map(child => child.nodeValue.trim()).join(" ");

console.log(output);

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!