Home > Mobile >  Extract <li> from string
Extract <li> from string

Time:11-16

I have to get all <li> body content which is inside in string. Cuz i have to map it.

let content = {
    para: `<p>A listing page is the public view of a listing. It should informations be designed to display all relevant information about a listing.</p>
          <p>Criteria:</p>
  
          <ul>
              <li>A should be informations displaying all the information related to the listing that user has created.</li>
              <li>A listing page should only be visible to any user if the listing has status: Published.</li>
              <li>A listing page need to be SEO optimised to have meta tags for title, short description and image.</li>
              <li>A listings owners should be a information link to their profile page, eg.&nbsp;<code>/profile/kansuler</code>&nbsp;if that user is owner of the project this listing is part of.</li>
              <li>A listing page should also display links to other listings that are part of the same project. Those listings should be displayed as &quot;Related listings&quot;.</li>
              <li>The url to a listing page should follow the pattern of the generated slug for that listing eg.&nbsp;<code>/listing/my-listing</code></li>
              <li>If the user is an admin, also display an admin description link to that listings edit page.</li>
          </ul>`}

I have to map all the data inside ul tag and then render it via props in js. Do not use dangrouslySetInnerHTML method.

CodePudding user response:

Try this :

If you want all the li in a single string :

let liContentWithoutRegex = content.para.split('<li>').map((li) => li.split('</li>')[0]);

If you want all the li in a array :

let liContentArrayWithoutRegex = content.para.split('<li>').map((li) => li.split('</li>')[0].split(' '));

CodePudding user response:

Try to use:

content.para.matchAll(/((?:<li>.*?<\/li>))/gm)
  • Related