# react-markup-string

> Find a specified string from the entire string, and replace it with a specified JSX.Element

Latest version **1.1.3** (published 2019-07-21) · MIT license · 0 weekly downloads

## Install

```sh
npm install react-markup-string
pnpm add react-markup-string
yarn add react-markup-string
bun add react-markup-string
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.1.3 |
| Published | 2019-07-21 |
| First published | 2019-04-15 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 9.3 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | morinaga |
| Maintainers | morinaga |
| Keywords | react, markup, string, replace, url, br, \n |

## Links

- npm: https://www.npmjs.com/package/react-markup-string
- Repository: https://github.com/morinaga-quijano/react-markup-string
- Homepage: https://github.com/morinaga-quijano/react-markup-string#readme
- Issues: https://github.com/morinaga-quijano/react-markup-string/issues
- npm.io page: https://npm.io/package/react-markup-string

## Dependencies (1)

- [react](https://npm.io/package/react.md) ^16.8.6

## Alternatives

- [mobx-react](https://npm.io/package/mobx-react.md) — 2.8M weekly downloads
- [rc-tree](https://npm.io/package/rc-tree.md) — 2.6M weekly downloads
- [@react-oauth/google](https://npm.io/package/@react-oauth/google.md) — 1.3M weekly downloads
- [@wagmi/connectors](https://npm.io/package/@wagmi/connectors.md) — 877.0K weekly downloads
- [vee-validate](https://npm.io/package/vee-validate.md) — 836.4K weekly downloads

## Recent versions

- 1.1.3 (latest) — 2019-07-21
- 1.1.2 — 2019-07-12
- 1.1.1 — 2019-07-12
- 1.1.0 — 2019-04-18
- 1.0.0 — 2019-04-16
- 0.2.1 — 2019-04-15
- 0.2.0 — 2019-04-15
- 0.1.2 — 2019-04-15
- 0.1.1 — 2019-04-15
- 0.1.0 — 2019-04-15

## README

# react-markup-string [![Build Status](https://travis-ci.org/morinaga-quijano/react-markup-string.svg?branch=master)](https://travis-ci.org/morinaga-quijano/react-markup-string)
Find a specified string from the entire string, and replace it with a specified JSX.Element

## Installation

```
npm i react-markup-string
```

## Usage
For example, if you want to replace a url in a string with `<a>` JSX.Element

You can write your code with Markup component like this (TypeScript) (see in [CodeSandbox](https://codesandbox.io/s/l9vwwxwymm?fontsize=14))

```TypeScript
const text = 'This url https://test.com should be replaced';

const regExp = /https?:\/\/[a-zA-Z0-9/.?_%=\\-]+/g;

const markuper = (matchText: string, key: number) => <a href={matchText} key={key}>{matchText}</a>;

const markuped = (
    <Markup regExp={regExp} markuper={markuper}>
        {text}
    </Markup>
);
/**
 * This url <a href="https://test.com">https://test.com</a> should be replaced
 */
```

If you also want to replace \n with `<br>` JSX.Element

Add regular expression to `regExp` and if statement to `markuper()`

```TypeScript
const text = 'https://test.com\nhttps://example.com';

// Add \n with |
const regExp = /https?:\/\/[a-zA-Z0-9/.?_%=\\-]+|\n/g;

// Add if statement to mark up the text with different elements
const markuper = (matchText: string, key: number) => {
    if (matchText.startsWith('http')) {
        return (
            <a href={matchText} key={key}>
                {matchText}
            </a>
        );
    } else {
        return <br key={key} />;
    }
};

const markuped = (
    <Markup regExp={regExp} markuper={markuper}>
        {text}
    </Markup>
);

```

### Custom matcher

You can also use custom matcher.
This is useful if there is some condition that is difficult to express in regular expression.

```TypeScript
const text = 'https://test.net & https://forbidden.org';

const allowedDomains = ['test.net', 'example.com', 'experiment.xyz'];

const matcher = (text: string) => {
    const regExp = /https?:\/\/([a-zA-Z0-9.]+)\/?[a-zA-Z0-9/.?_%=\\-]*/g;

    const result: MatchResult[] = [];

    let match: RegExpExecArray | null;

    while ((match = regExp.exec(text)) !== null) {
        if (allowedDomains.includes(match[1])) {
            result.push({
                index: match.index,
                text: match[0]
            });
        }    
    }

    return result;
};

const markuper = (matchText: string, key: number) => (
    <a href={matchText} key={key}>
        {matchText}
    </a>
);

const markuped = (
    <Markup matcher={matcher} markuper={markuper}>
        {text}
    </Markup>
);
/** 
 * <a href="https://test.net">https://test.net</a> & https://forbidden.org
 */
```

---
_Source: https://npm.io/package/react-markup-string · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
