问题描述
|
我想检查一下这3个字符串中的任何一个:
popularity
rating
views
存在于存储在名为“ 1”的变量中的字符串中。如果答案是肯定的,那么我想用单词“ 2”替换找到的字符串。
有什么优雅的方法可以做到这一点?
解决方法
您可以使用正则表达式:
theUrl = theUrl.replace(/\\b(popularity|rating|views)\\b/g,\'REPLACED\')
, 方式如下:
theUrl = theUrl.replace(\"popularity\",\"REPLACED\").replace(\"rating\",\"REPLACED\").replace(\"views\",\"REPLACED\");