Skip to content

Commit 7867d67

Browse files
committed
Update readme
1 parent d82fe4d commit 7867d67

File tree

1 file changed

+40
-2
lines changed

1 file changed

+40
-2
lines changed

readme.md

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,42 @@ const HighlightNumbers = React.createClass({
6363
});
6464
```
6565

66+
### Multiple replacements on a single string
67+
68+
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:
69+
70+
```js
71+
const reactStringReplace = require('react-string-replace')
72+
73+
const text = 'Hey @ian_989, check out this link https://github.com/iansinnott/. Hope to see you at #reactconf';
74+
let replacedText;
75+
76+
// Match URLs
77+
replacedText = replaceString(originalTweet, /(https?:\/\/\S+)/g, match => (
78+
<a href={match}>{match}</a>
79+
));
80+
81+
// Match @-mentions
82+
replacedText = replaceString(replacedText, /@(\w+)/g, match => (
83+
<a href={`https://twitter.com/${match}`}>@{match}</a>
84+
));
85+
86+
// Match hashtags
87+
replacedText = replaceString(replacedText, /#(\w+)/g, match => (
88+
<a href={`https://twitter.com/hashtag/${match}`}>#{match}</a>
89+
));
90+
91+
// => [
92+
// 'Hey ',
93+
// <a href='https://twitter.com/ian_989'>@ian_989</a>
94+
// ', check out this link ',
95+
// <a href='https://github.com/iansinnott/'>{https://github.com/iansinnott/}</a>,
96+
// '. Hope to see you at ',
97+
// <a href='https://twitter.com/hashtag/reactconf'>#reactconf</a>,
98+
// '',
99+
// ];
100+
```
101+
66102
### Full Example
67103

68104
See the `example/` directory for a runnable example.
@@ -77,9 +113,11 @@ I wanted an easy way to highlight strings within React **without** breaking Reac
77113

78114
#### string
79115

80-
Type: `string`
116+
Type: `string|array`
117+
118+
The string or array you would like to do replacement on.
81119

82-
The string you would like to do replacement on.
120+
**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.
83121

84122
#### match
85123

0 commit comments

Comments
 (0)