![](/img/trans.png)
[英]Wrap words in a string with strong tags given a list of words once and in order
[英]How to wrap words with anchor tags in react if string contains certain symbols
嘿伙計們正在使用#hashtags 和@mentions 處理博客內容,我想檢查一個字符串中的所有單詞,如果這些單詞有@mention 或#tags 用錨標記包裹它們,所以incase 我有類似“hello @看門狗是你@example 和#trends #football"
所以我想包裝成類似@watchdog @exmaple #exmaple的東西
我正在使用反應 js
export const text = (text) =>{
const [linktext,setLinkText] = useState('')
useEffect(()=>{
},[])
return (
<div>{text}</div>
)
}
export const text = (text) =>{
const [linktext,setLinkText] = useState("hello @watchdog are you with @example and #trends #football")
useEffect(()=>{
},[])
return (
<div>{linktext.split(" ").map(word => (
(word.includes("@") || word.includes("#"))
?
<a>{word} </a>
:
<p>{word}</p>
))}</div>
)
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.