react-native-parsed-text icon indicating copy to clipboard operation
react-native-parsed-text copied to clipboard

Change words in text?

Open hristowwe opened this issue 4 years ago • 1 comments
trafficstars

If i have this text like example in github

    return (
      <View style={styles.container}>
        <ParsedText
          style={styles.text}
          parse={
            [
              {type: 'url',                       style: styles.url, onPress: this.handleUrlPress},
              {type: 'phone',                     style: styles.phone, onPress: this.handlePhonePress},
              {type: 'email',                     style: styles.email, onPress: this.handleEmailPress},
              {pattern: /Bob|David/,              style: styles.name, onPress: this.handleNamePress},
              {pattern: /\[(@[^:]+):([^\]]+)\]/i, style: styles.username, onPress: this.handleNamePress, renderText: this.renderText},
              {pattern: /42/,                     style: styles.magicNumber},
              {pattern: /#(\w+)/,                 style: styles.hashTag},
            ]
          }
          childrenProps={{allowFontScaling: false}}
        >
          Hello this is an example of the ParsedText, links like http://www.google.com or http://www.facebook.com are clickable and phone number 444-555-6666 can call too.
          But you can also do more with this package, for example Bob will change style and David too. [email protected]
          And the magic number is 42!
          #react #react-native
        </ParsedText>

And i want to change value for #react how can i do that?

hristowwe avatar Sep 14 '21 12:09 hristowwe

I realise this issue is very old, but for completeness' sake, I think renderText is what you were looking for:

{
  pattern: /#(\w+)/,
  style: styles.hashTag,
  renderText: (s, m) => s.replace('#react', '#whatever'),
},

d11wtq avatar Nov 29 '23 11:11 d11wtq