Skip to content

Commit 8945a23

Browse files
committed
docs: update README
- Add npm/yarn install options alongside bun - Remove obsolete index workaround (fixed in v2.0.0) - Modernize React example (createClass → function component) - Document v2.0.0 breaking change in API Stability section - Fix test file reference (test.js → index.test.js)
1 parent 4a03802 commit 8945a23

File tree

1 file changed

+46
-41
lines changed

1 file changed

+46
-41
lines changed

readme.md

Lines changed: 46 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,21 @@ A simple way to safely do string replacement with React components. Zero depende
99
## Install
1010

1111
```sh
12+
npm install react-string-replace
13+
# or
1214
yarn add react-string-replace
15+
# or
16+
bun add react-string-replace
1317
```
1418

15-
1619
## Usage
1720

1821
First, import the lib. Both `require` and `import` are supported.
1922

2023
```js
21-
import reactStringReplace from 'react-string-replace';
24+
import reactStringReplace from "react-string-replace";
2225
// OR
23-
const reactStringReplace = require('react-string-replace')
26+
const reactStringReplace = require("react-string-replace");
2427
```
2528

2629
Examples will use `import` since it is more common in the React ecosystem.
@@ -34,24 +37,17 @@ reactStringReplace('whats your name', 'your', (match, i) => (
3437
<span>{match}</span>
3538
));
3639
// => [ 'whats ', <span>your</span>, ' name' ]
37-
38-
39-
// Note that indexing [i] here starts at 1, not 0
40-
// If you need to change this behavior for now try the workaround:
41-
let count = -1;
42-
reactStringReplace("the more the better", "the", (match, i) => (
43-
count ++
44-
<span>{match}</span>
45-
));
4640
```
4741

4842
### More realistic example
4943

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

5246
```js
53-
reactStringReplace('Apt 111, phone number 5555555555.', /(\d+)/g, (match, i) => (
54-
<span key={i} style={{ color: 'red' }}>{match}</span>
47+
reactStringReplace("Apt 111, phone number 5555555555.", /(\d+)/g, (match, i) => (
48+
<span key={i} style={{ color: "red" }}>
49+
{match}
50+
</span>
5551
));
5652
// =>
5753
// [
@@ -65,46 +61,53 @@ reactStringReplace('Apt 111, phone number 5555555555.', /(\d+)/g, (match, i) =>
6561

6662
### Within a React component
6763

68-
```js
69-
import reactStringReplace from 'react-string-replace';
70-
71-
const HighlightNumbers = React.createClass({
72-
render() {
73-
const content = 'Hey my number is 555-555-5555.';
74-
return (
75-
<div>
76-
{reactStringReplace(content, /(\d+)/g, (match, i) => (
77-
<span key={i} style={{ color: 'red' }}>{match}</span>
78-
))}
79-
</div>
80-
);
81-
},
82-
});
64+
```jsx
65+
import reactStringReplace from "react-string-replace";
66+
67+
function HighlightNumbers() {
68+
const content = "Hey my number is 555-555-5555.";
69+
return (
70+
<div>
71+
{reactStringReplace(content, /(\d+)/g, (match, i) => (
72+
<span key={i} style={{ color: "red" }}>
73+
{match}
74+
</span>
75+
))}
76+
</div>
77+
);
78+
}
8379
```
8480

8581
### Multiple replacements on a single string
8682

8783
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:
8884

8985
```js
90-
import reactStringReplace from 'react-string-replace';
86+
import reactStringReplace from "react-string-replace";
9187

92-
const text = 'Hey @ian_sinn, check out this link https://github.com/iansinnott/ Hope to see you at #reactconf';
88+
const text =
89+
"Hey @ian_sinn, check out this link https://github.com/iansinnott/ Hope to see you at #reactconf";
9390
let replacedText;
9491

9592
// Match URLs
9693
replacedText = reactStringReplace(text, /(https?:\/\/\S+)/g, (match, i) => (
97-
<a key={match + i} href={match}>{match}</a>
94+
<a key={match + i} href={match}>
95+
{match}
96+
</a>
9897
));
9998

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

105106
// Match hashtags
106107
replacedText = reactStringReplace(replacedText, /#(\w+)/g, (match, i) => (
107-
<a key={match + i} href={`https://twitter.com/hashtag/${match}`}>#{match}</a>
108+
<a key={match + i} href={`https://twitter.com/hashtag/${match}`}>
109+
#{match}
110+
</a>
108111
));
109112

110113
// => [
@@ -142,14 +145,14 @@ The string or array you would like to do replacement on.
142145

143146
Type: `regexp|string`
144147

145-
The string or RegExp you would like to replace within `string`.
148+
The string or RegExp you would like to replace within `string`.
146149

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

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

151154
```js
152-
reactStringReplace('hey hey you', /(hey)/g, () => <span>hey</span>);
155+
reactStringReplace("hey hey you", /(hey)/g, () => <span>hey</span>);
153156
```
154157

155158
#### replacementFunction
@@ -160,26 +163,28 @@ The replacer function to run each time `match` is found. This function will be p
160163

161164
```js
162165
const replacementFunction = (match, index, offset) => <span key={index}>{match}</span>;
163-
reactStringReplace('hey hey you', /(hey)/g, replacementFunction);
166+
reactStringReplace("hey hey you", /(hey)/g, replacementFunction);
164167
```
165168

166169
#### count
167170

168171
Type: `number`
169172

170-
The number of substitutions to perform - for example if `count` is 1, then only the first occurrence of the string will be replaced.
173+
The number of substitutions to perform - for example if `count` is 1, then only the first occurrence of the string will be replaced.
171174

172175
Example: Replace first occurrence of `'hey'` with `<span>hey</span>`
173176

174177
```js
175-
reactStringReplace('hey hey you', 'hey', () => <span>hey</span>, 1)
178+
reactStringReplace("hey hey you", "hey", () => <span>hey</span>, 1);
176179
```
177180

178181
## API Stability
179182

180-
With v1.0.0 the API is considered stable and should be considered production ready. Pull requests are still welcome but there is currently no intent to make changes to this lib other than bug fixes (please submit an issue if you find something!).
183+
The API is stable and production ready.
184+
185+
**v2.0.0 Breaking Change:** The `index` parameter passed to the replacement function now starts at 0 and increments by 1 (previously it started at 1 and incremented by 2).
181186

182-
For details on API tests see [the tests file](./test.js).
187+
For details on API tests see [the tests file](./index.test.js).
183188

184189
## License
185190

0 commit comments

Comments
 (0)