简体   繁体   中英

Replace multiple words to other words in a string from a list of words

This question is similar to: Replace multiple words in a string from a list of words

The accepted solution there is the following

string cleaned = Regex.Replace(input, "\\b" + string.Join("\\b|\\b",BAD_WORDS) + "\\b", "")

However, in my case I want to replace each "word" from the list with its bold version (basically add in the beginning and at the end)

Regex.Replace(input, $@"\b(?:{string.Join("|",BAD_WORDS)})\b", "<b>$&</b>")

这是WiktorStribiżew建议的解决方案-完美地工作

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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