You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

ReactJS中如何仅渲染HTML字符串里的<h1>标签内容

Solution to Extract Only
Content from HTML String in React

Hey there! Let's solve this problem where you need to pull just the

content from your HTML string. Here are two reliable approaches:

This is the safest and most robust way, especially if your HTML might have variations like attributes in the

tag or nested elements inside it. The browser's built-in DOMParser can parse the HTML string into a document, allowing you to query for the
element and extract its text.

Here's how to implement it in your React component:

import React from 'react';

function EventComponent() {
  const textHtml = "<h1>What events are you looking for today?</h1> <p>Find more events you want!</p>";

  // Function to extract h1 content
  const getH1Text = (html) => {
    const parser = new DOMParser();
    const doc = parser.parseFromString(html, 'text/html');
    const h1Element = doc.querySelector('h1');
    // Return the text content if h1 exists, else empty string
    return h1Element ? h1Element.textContent.trim() : '';
  };

  const h1Content = getH1Text(textHtml);

  return (
    <div>
      {/* Render just the extracted text */}
      <h1>{h1Content}</h1>
    </div>
  );
}

export default EventComponent;

Why this works:

  • DOMParser creates a temporary DOM document from your HTML string.
  • querySelector('h1') finds the first
    element in the parsed document.
  • textContent gets all the text inside the
    , ignoring any nested tags (if present).
  • Finally, we render the plain text inside a React
    element (or any tag you prefer).

Method 2: Regular Expression (For Simple, Predictable HTML)

If you're certain your HTML string will always have a simple

tag with no attributes or nested elements, you can use a regex to extract the content. However, note that regex isn't designed for parsing HTML and can break if the HTML structure changes (like adding classes to the
).

Example code:

import React from 'react';

function EventComponent() {
  const textHtml = "<h1>What events are you looking for today?</h1> <p>Find more events you want!</p>";
  
  // Regex to match content inside <h1> tags (case-insensitive)
  const h1Match = textHtml.match(/<h1>(.*?)<\/h1>/i);
  const h1Content = h1Match ? h1Match[1].trim() : '';

  return (
    <div>
      <h1>{h1Content}</h1>
    </div>
  );
}

export default EventComponent;

Important Notes:

  • Avoid using dangerouslySetInnerHTML for the full HTML string unless absolutely necessary, as it can expose your app to XSS attacks if the HTML comes from an untrusted source. Both methods above extract plain text, which is safe to render directly.
  • If your HTML has multiple
    tags, querySelector('h1') will get the first one. To get all, use querySelectorAll('h1') and loop through them.

内容的提问来源于stack exchange,提问作者BayBay

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.27 19:12:42