简体   繁体   English

React javascript 检查字符串是否包含特定单词并将诸如链接反应路由器/样式之类的内容添加到特定单词

[英]React javascript check if string contains specific word and add something like link react router / styles to specific word

is there any solution how to check strings(ex:Google) and add Link react router into specific word(ex:Google)?是否有任何解决方案如何检查字符串(例如:Google)并将 Link react 路由器添加到特定单词(例如:Google)中?

example like this photo像这张照片的例子

before

前

after

后

this is my very simple code这是我非常简单的代码

import React, { Component } from "react";
import { Link } from "react-router-dom";

class App extends Component {
  state = {
    text: "Google is my Friend, Google is Search Engine, Thanks to google"
    website: "Google Friend"
  };

  render() {
    let text = null;
    if (this.state.text.includes(this.state.website)) {
      return (text = (
        <div>
          <Link to={"/example"}>Google </Link> then state text
        </div>
      ));
    }
    return <div>{text}</div>;
  }
}

export default App;

it would be more helpful if you can show me how to add style inside specific word(ex: Google) like changing color, or adding bold/italic or anything如果您能告诉我如何在特定单词(例如:Google)中添加样式,例如更改颜色,或添加粗体/斜体或任何内容,那将会更有帮助

thanks谢谢

You can do it like this:你可以这样做:

{this.state.text.split(" ").map(text => {
   return text === "Google" ? <Link to="/google">Google</Link> : text;
})}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM