All Projects → appfigures → string-replace-to-array

appfigures / string-replace-to-array

Licence: MIT License
Like Javascript's string.replace, but accepts and returns an array

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to string-replace-to-array

React Workshop
⚒ 🚧 This is a workshop for learning how to build React Applications
Stars: ✭ 114 (+500%)
Mutual labels:  jsx, react-components
Dataformsjs
🌟 DataFormsJS 🌟 A minimal JavaScript Framework and standalone React and Web Components for rapid development of high quality websites and single page applications.
Stars: ✭ 95 (+400%)
Mutual labels:  jsx, react-components
React String Replace
A simple way to safely do string replacement with React components
Stars: ✭ 360 (+1794.74%)
Mutual labels:  react-components, string-manipulation
react-ui-components
React UI Components (npm @assenti/rui-components)
Stars: ✭ 21 (+10.53%)
Mutual labels:  jsx, react-components
React Border Wrapper
A wrapper for placing elements along div borders.
Stars: ✭ 147 (+673.68%)
Mutual labels:  jsx, react-components
React Structured Data
React Structured Data provides an easy way to add structured data to your React apps
Stars: ✭ 120 (+531.58%)
Mutual labels:  jsx, react-components
React Jsx Parser
A React component which can parse JSX and output rendered React Components.
Stars: ✭ 394 (+1973.68%)
Mutual labels:  jsx, react-components
React Code Blocks
React code blocks and code snippet components
Stars: ✭ 135 (+610.53%)
Mutual labels:  jsx, react-components
React Native demo
react-native实现网易新闻和美团,实现大部分页面。使用最新的react-navigation等组件,同时支持安卓和iOS设备。
Stars: ✭ 237 (+1147.37%)
Mutual labels:  jsx, react-components
React-Netflix-Clone
A Fully Responsive clone of Netflix website built using React.JS as a Front-end & Firebase as a Back-end.
Stars: ✭ 91 (+378.95%)
Mutual labels:  jsx, react-components
reactjs-portfolio
Welcome to my portfolio react.js repository page.
Stars: ✭ 109 (+473.68%)
Mutual labels:  react-components
clarity-react
React Components for VMware Clarity UI and Clarity Design
Stars: ✭ 33 (+73.68%)
Mutual labels:  react-components
zaftig
~2kB css in js: z`display flex` // .zjsdkk43-1
Stars: ✭ 15 (-21.05%)
Mutual labels:  jsx
how-react-hooks-work
Understand how React-hook really behaves, once and for all!
Stars: ✭ 73 (+284.21%)
Mutual labels:  react-components
pityWeb
🎉一个持续迭代的开源接口测试平台(前端),欢迎大家多提issue多给反馈。 求star⭐,我会努力更新下去的!
Stars: ✭ 25 (+31.58%)
Mutual labels:  jsx
react-admin-nest
React和Ant Design和 Nest.js 和 Mysql 构建的后台通用管理系统。持续更新。
Stars: ✭ 123 (+547.37%)
Mutual labels:  react-components
indent.js
Pure code indentation for jsx, tsx, ts, js, html, css, less, scss.
Stars: ✭ 55 (+189.47%)
Mutual labels:  jsx
react-multiversal
React components that works everywhere (iOS, Android, Web, Node)
Stars: ✭ 43 (+126.32%)
Mutual labels:  react-components
design-system
A resource for creating user interfaces based on brand, UX, and dev principles
Stars: ✭ 17 (-10.53%)
Mutual labels:  react-components
react-recurly
React components for Recurly.js
Stars: ✭ 38 (+100%)
Mutual labels:  react-components

String replace to array

string-replace-to-array MIT license on NPM string-replace-to-array on NPM Build Status string-replace-to-array total downloads on NPM string-replace-to-array monthly downloads on NPM

Works just like String.prototype.replace but outputs an array instead of a string.

Why?

We built this for use with React, but it's very generic and doesn't depend on any environment. Consider the following scenario.

Given this string:

var content = 'Hello\nworld'

and this React markup:

<span>{ content }</span>

We'll get this output:

Hello world

The newline character is ignored when the browser renders the resulting html.

The solution is to replace \n with <br>:

<span>{ replace(content, '\n', <br>) }</span>

and the output will be:

<span>Hello</br>world</span>

When rendered:

Hello
world

Now the newline will be rendered properly. Yay!

Example usage

Simple example

var replace = require('string-replace-to-array')
replace('Hello Amy', 'Amy', { name: 'Amy' })
// output: ['Hello ', { name: 'Amy' }]

Full example

replace(
  'Hello Hermione Granger...',
  /(Hermione) (Granger)/g,
  function (fullName, firstName, lastName, offset, string) {
    return <Person firstName={ firstName } lastName={ lastName } />
  }
)

// output: ['Hello ', <Person firstName="Hermione" lastName="Granger" />, ...]

For a real-life example check out react-easy-emoji, where this this is used to replace emoji unicode characters with <img> tags.

Installation

npm install --save string-replace-to-array

API

(string, regexp|substr, newValue|function) => array

The API mimics String.prototype.replace. The only differences are:

  • The replacer (third parameter) doesn't have to be a string
  • Returns an array instead of a string

Inspiration

Mainly inspired by this conversation: https://github.com/facebook/react/issues/3386

Why not use react-replace-string?

Because we needed the full API of String.replace, especially the regex match parameters which get passed to the replace function.

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].