All Projects → iansinnott → React String Replace

iansinnott / React String Replace

Licence: mit
A simple way to safely do string replacement with React components

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to React String Replace

string-replace-to-array
Like Javascript's string.replace, but accepts and returns an array
Stars: ✭ 19 (-94.72%)
Mutual labels:  react-components, string-manipulation
Tiny Utf8
Unicode (UTF-8) capable std::string
Stars: ✭ 322 (-10.56%)
Mutual labels:  string-manipulation
Cuerdas
String manipulation library for Clojure(Script)
Stars: ✭ 272 (-24.44%)
Mutual labels:  string-manipulation
React Native Pagination
Animated Pagination For React Native's ListView, FlatList, and SectionList
Stars: ✭ 296 (-17.78%)
Mutual labels:  react-components
Chakra Ui
⚡️ Simple, Modular & Accessible UI Components for your React Applications
Stars: ✭ 22,745 (+6218.06%)
Mutual labels:  react-components
React Shortcuts
Manage keyboard shortcuts from one place
Stars: ✭ 302 (-16.11%)
Mutual labels:  react-components
React Event Components
🛰 A set of React components designed to handle global events (interval, keyboard, touch, mouse, etc)
Stars: ✭ 271 (-24.72%)
Mutual labels:  react-components
Re Flex
Resizable Flex layout container components for advanced React web applications
Stars: ✭ 349 (-3.06%)
Mutual labels:  react-components
Reaptcha
Google reCAPTCHA v2 for React
Stars: ✭ 329 (-8.61%)
Mutual labels:  react-components
Hyper React
The project has moved to Hyperstack!!
Stars: ✭ 295 (-18.06%)
Mutual labels:  react-components
React Stripe Elements
Moved to stripe/react-stripe-js.
Stars: ✭ 3,047 (+746.39%)
Mutual labels:  react-components
Eo Locale
🌏Internationalize js apps 👔Elegant lightweight library based on Internationalization API
Stars: ✭ 290 (-19.44%)
Mutual labels:  react-components
React Js Pagination
Stars: ✭ 308 (-14.44%)
Mutual labels:  react-components
Reactackle
Open-source components library built with React and Styled-Components.
Stars: ✭ 278 (-22.78%)
Mutual labels:  react-components
Airframe React
Free Open Source High Quality Dashboard based on Bootstrap 4 & React 16: http://dashboards.webkom.co/react/airframe
Stars: ✭ 3,659 (+916.39%)
Mutual labels:  react-components
React Recomponent
🥫 Reason-style reducer components for React using ES6 classes.
Stars: ✭ 272 (-24.44%)
Mutual labels:  react-components
Formsy React Components
Bootstrap components for a formsy-react form.
Stars: ✭ 290 (-19.44%)
Mutual labels:  react-components
Chakra Ui
⚡️Simple, Modular & Accessible UI Components for your React Applications
Stars: ✭ 295 (-18.06%)
Mutual labels:  react-components
Rimble Ui
React components that implement Rimble's Design System.
Stars: ✭ 357 (-0.83%)
Mutual labels:  react-components
React Awesome Reveal
React components to add reveal animations using the Intersection Observer API and CSS Animations.
Stars: ✭ 346 (-3.89%)
Mutual labels:  react-components

React String Replace

Build Status react-string-replace.js on NPM

A simple way to safely do string replacement with React components

Aka turn a string into an array of React components

Install

$ npm install --save react-string-replace

Usage

Simple Example

const reactStringReplace = require('react-string-replace')
reactStringReplace('whats your name', 'your', (match, i) => (
  <span>{match}</span>
));
// => [ 'whats ', <span>your</span>, ' name' ]

More realistic example

Highlight all digits within a string by surrounding them in span tags:

reactStringReplace('Apt 111, phone number 5555555555.', /(\d+)/g, (match, i) => (
  <span key={i} style={{ color: 'red' }}>{match}</span>
));
// =>
// [
//   'Apt ',
//   <span style={{ color: 'red' }}>111</span>,
//   ', phone number ',
//   <span style={{ color: 'red' }}>5555555555</span>,
//   '.'
// ]

Within a React component

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

const HighlightNumbers = React.createClass({
  render() {
    const content = 'Hey my number is 555-555-5555.';
    return (
      <div>
        {reactStringReplace(content, /(\d+)/g, (match, i) => (
          <span key={i} style={{ color: 'red' }}>{match}</span>
        ))}
      </div>
    );
  },
});

Multiple replacements on a single string

You can run multiple replacements on one string by calling the function multiple times on the returned result. For instance, if we want to match URLs, @-mentions and hashtags in a string we could do the following:

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

const text = 'Hey @ian_sinn, check out this link https://github.com/iansinnott/ Hope to see you at #reactconf';
let replacedText;

// Match URLs
replacedText = reactStringReplace(text, /(https?:\/\/\S+)/g, (match, i) => (
  <a key={match + i} href={match}>{match}</a>
));

// Match @-mentions
replacedText = reactStringReplace(replacedText, /@(\w+)/g, (match, i) => (
  <a key={match + i} href={`https://twitter.com/${match}`}>@{match}</a>
));

// Match hashtags
replacedText = reactStringReplace(replacedText, /#(\w+)/g, (match, i) => (
  <a key={match + i} href={`https://twitter.com/hashtag/${match}`}>#{match}</a>
));

// => [
//   'Hey ',
//   <a href='https://twitter.com/ian_sinn'>@ian_sinn</a>
//   ', check out this link ',
//   <a href='https://github.com/iansinnott/'>https://github.com/iansinnott/</a>,
//   '. Hope to see you at ',
//   <a href='https://twitter.com/hashtag/reactconf'>#reactconf</a>,
//   '',
// ];

Full Example

See the example/ directory for a runnable example.

Why?

I wanted an easy way to do string replacement a la String.prototype.replace within React components without breaking React's built in string escaping functionality. This meant standard string replacement combined with dangerouslySetInnerHTML was out of the question.

API

reactStringReplace(string, match, func)

string

Type: string|array

The string or array you would like to do replacement on.

NOTE: When passed an array this is the same as running the replacement on every string within the array. Any non-string values in the array will be left untouched.

match

Type: regexp|string

The string or RegExp you would like to replace within string.

NOTE: When using a RegExp you MUST include a capturing group. (/(hey)/g is ok, /hey/g is not.)

Example: Replace all occurrences of 'hey' with <span>hey</span>

reactStringReplace('hey hey you', /(hey)/g, () => <span>hey</span>);

func

Type: function

The replacer function to run each time match is found. This function will be passed the matching string and an index which can be used for adding keys to replacement components if necessary. Character offset identifies the position of match start in the provided text.

const func = (match, index, offset) => <span key={index}>{match}</span>;
reactStringReplace('hey hey you', /(hey)/g, func);

License

MIT © Ian Sinnott

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].