Socket
Socket
Sign inDemoInstall

react-string-replace

Package Overview
Dependencies
Maintainers
1
Versions
16
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

react-string-replace

String#replace for React components


Version published
Maintainers
1
Created

What is react-string-replace?

The react-string-replace npm package allows you to easily search and replace parts of a string with React components. This is particularly useful for rendering dynamic content within strings, such as highlighting keywords, linking URLs, or embedding custom components.

What are react-string-replace's main functionalities?

Basic String Replacement

This feature allows you to replace a specific substring within a string with a React component. In this example, the word 'world' is replaced with a <strong> component.

const React = require('react');
const reactStringReplace = require('react-string-replace');

const text = 'Hello, world!';
const replacedText = reactStringReplace(text, 'world', (match, i) => (
  <strong key={i}>{match}</strong>
));

console.log(replacedText); // ['Hello, ', <strong key="0">world</strong>, '!']

Replacing Multiple Substrings

This feature allows you to replace multiple occurrences of a substring within a string with a React component. In this example, all occurrences of the word 'Hello' are replaced with an <em> component.

const React = require('react');
const reactStringReplace = require('react-string-replace');

const text = 'Hello, world! Hello, universe!';
const replacedText = reactStringReplace(text, 'Hello', (match, i) => (
  <em key={i}>{match}</em>
));

console.log(replacedText); // [<em key="0">Hello</em>, ', world! ', <em key="1">Hello</em>, ', universe!']

Replacing with Custom Components

This feature allows you to replace substrings matching a regular expression with custom React components. In this example, URLs are replaced with <a> components.

const React = require('react');
const reactStringReplace = require('react-string-replace');

const text = 'Visit https://example.com for more info.';
const replacedText = reactStringReplace(text, /https?:\/\/\S+/g, (match, i) => (
  <a href={match} key={i} target="_blank" rel="noopener noreferrer">{match}</a>
));

console.log(replacedText); // ['Visit ', <a href="https://example.com" key="0" target="_blank" rel="noopener noreferrer">https://example.com</a>, ' for more info.']

Other packages similar to react-string-replace

Keywords

FAQs

Package last updated on 15 Jun 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc