账号密码登录
微信安全登录
微信扫描二维码登录

登录后绑定QQ、微信即可实现信息互通

手机验证码登录
找回密码返回
邮箱找回 手机找回
注册账号返回
其他登录方式
分享
  • 收藏
    X
    react native中,我想把字符串中某个关键字替换成红色,应该怎么做呢?
    118
    0

    原先的思路是利用repace把关键字替换,然后在两端加上<Text>上色.但是发现replace之后<Text>被识别为字符串了.

    问一下要实现标题所述的效果应该用什么办法呢?

      _replaceSeachWordToRed=(index,text)=>{
        let str = this.state.searchWord;
        let reg = new RegExp(str, "g");
        let redStr=()=>{return(<Text style={{color:"red"}}>str</Text>)};
        let replacedStr=text.replace(reg, redStr);
        return <Text style={[styles.contentFace]}>{index+1}. {replacedStr}</Text>;
      };
    2
    打赏
    收藏
    点击回答
        全部回答
    • 0
    • 黄昏下的影子 普通会员 1楼

      在React Native中,你可以使用React Native的内置字符串方法replacetoLowerCase来实现这个功能。以下是一个简单的示例:

      ```jsx import React, { Component } from 'react'; import { View, TextInput, StyleSheet, TouchableOpacity } from 'react-native';

      class App extends Component { constructor(props) { super(props); this.state = { inputValue: '', }; }

      handleInputChange = (text) => { this.setState({ inputValue: text }); }

      replaceKeywordInValue = (text) => { const replacedValue = text.replace(/(\w+)(.*)/g, (old, newWord) => { return ${newWord} ${old}; }); this.setState({ inputValue: replacedValue }); }

      render() { return ( this.replaceKeywordInValue(this.state.inputValue)}> Replace Keyword ); } }

      const styles = StyleSheet.create({ container: { flex: 1, backgroundColor: '#fff', alignItems: 'center', justifyContent: 'center', }, input: { height: 40, borderColor: '#ccc', borderWidth: 1, margin: 10, }, button: { backgroundColor: '#007bff', padding: 10, borderRadius: 5, alignItems: 'center', justifyContent: 'center', }, });

      export default App; ```

      在这个示例中,我们首先在TextInput组件中设置value属性,然后在handleInputChange方法中更新inputValue。然后,我们使用replaceKeywordInValue方法来替换文本中的关键字。最后,我们在TouchableOpacity组件中点击按钮来替换文本中的关键字。

    • 凋谢了灿烂 普通会员 2楼

      在React Native中,你可以使用React Native的内置字符串方法replacetoLowerCase来实现这个功能。以下是一个简单的示例:

      ```jsx import React, { Component } from 'react'; import { View, TextInput, StyleSheet, TouchableOpacity } from 'react-native';

      class App extends Component { constructor(props) { super(props); this.state = { inputValue: '', }; }

      handleInputChange = (text) => { this.setState({ inputValue: text }); }

      replaceKeywordInValue = (text) => { const replacedValue = text.replace(/(\w+)(.*)/g, (old, newWord) => { return ${newWord} ${old}; }); this.setState({ inputValue: replacedValue }); }

      render() { return ( this.replaceKeywordInValue(this.state.inputValue)}> Replace Keyword ); } }

      const styles = StyleSheet.create({ container: { flex: 1, backgroundColor: '#fff', alignItems: 'center', justifyContent: 'center', }, input: { height: 40, borderColor: '#ccc', borderWidth: 1, margin: 10, }, button: { backgroundColor: '#007bff', padding: 10, borderRadius: 5, alignItems: 'center', justifyContent: 'center', }, });

      export default App; ```

      在这个示例中,我们首先在TextInput组件中设置value属性,然后在handleInputChange方法中更新inputValue。然后,我们使用replaceKeywordInValue方法来替换文本中的关键字。最后,我们在TouchableOpacity组件中点击按钮来替换文本中的关键字。

    • 往日情怀。 普通会员 3楼

      在React Native中,你可以使用React Native的内置字符串方法replacetoLowerCase来实现这个功能。以下是一个简单的示例:

      ```jsx import React, { Component } from 'react'; import { View, TextInput, StyleSheet, TouchableOpacity } from 'react-native';

      class App extends Component { constructor(props) { super(props); this.state = { inputValue: '', }; }

      handleInputChange = (text) => { this.setState({ inputValue: text }); }

      replaceKeywordInValue = (text) => { const replacedValue = text.replace(/(\w+)(.*)/g, (old, newWord) => { return ${newWord} ${old}; }); this.setState({ inputValue: replacedValue }); }

      render() { return ( this.replaceKeywordInValue(this.state.inputValue)}> Replace Keyword ); } }

      const styles = StyleSheet.create({ container: { flex: 1, backgroundColor: '#fff', alignItems: 'center', justifyContent: 'center', }, input: { height: 40, borderColor: '#ccc', borderWidth: 1, margin: 10, }, button: { backgroundColor: '#007bff', padding: 10, borderRadius: 5, alignItems: 'center', justifyContent: 'center', }, });

      export default App; ```

      在这个示例中,我们首先在TextInput组件中设置value属性,然后在handleInputChange方法中更新inputValue。然后,我们使用replaceKeywordInValue方法来替换文本中的关键字。最后,我们在TouchableOpacity组件中点击按钮来替换文本中的关键字。

    更多回答
    扫一扫访问手机版
    • 回到顶部
    • 回到顶部