- 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>;
}; - 共 0 条
- 全部回答
-
黄昏下的影子 普通会员 1楼
在React Native中,你可以使用React Native的内置字符串方法
replace和toLowerCase来实现这个功能。以下是一个简单的示例:```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的内置字符串方法
replace和toLowerCase来实现这个功能。以下是一个简单的示例:```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的内置字符串方法
replace和toLowerCase来实现这个功能。以下是一个简单的示例:```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组件中点击按钮来替换文本中的关键字。
- 扫一扫访问手机版
回答动态

- 神奇的四哥:发布了悬赏问题阿里云幻兽帕鲁服务器更新之后。服务器里面有部分玩家要重新创建角色是怎么回事啊?预计能赚取 0积分收益

- 神奇的四哥:发布了悬赏问题函数计算不同地域的是不能用内网吧?预计能赚取 0积分收益

- 神奇的四哥:发布了悬赏问题ARMS可以创建多个应用嘛?预计能赚取 0积分收益

- 神奇的四哥:发布了悬赏问题在ARMS如何申请加入公测呀?预计能赚取 0积分收益

- 神奇的四哥:发布了悬赏问题前端小程序接入这个arms具体是如何接入监控的,这个init方法在哪里进行添加?预计能赚取 0积分收益

- 神奇的四哥:发布了悬赏问题阿里云幻兽帕鲁服务器刚到期,是不是就不能再导出存档了呢?预计能赚取 0积分收益

- 神奇的四哥:发布了悬赏问题阿里云幻兽帕鲁服务器的游戏版本不兼容 尝试更新怎么解决?预计能赚取 0积分收益

- 神奇的四哥:发布了悬赏问题阿里云幻兽帕鲁服务器服务器升级以后 就链接不上了,怎么办?预计能赚取 0积分收益

- 神奇的四哥:发布了悬赏问题阿里云幻兽帕鲁服务器转移以后服务器进不去了,怎么解决?预计能赚取 0积分收益

- 神奇的四哥:发布了悬赏问题阿里云幻兽帕鲁服务器修改参数后游戏进入不了,是什么情况?预计能赚取 0积分收益
- 回到顶部
- 回到顶部

