只是想知道在字符串上替换就地匹配的最佳方法。
value.replace("bob", "fred");
例如,可行,但我希望将“bob”的每个实例替换为我存储在数组中的随机字符串。只是进行正则表达式匹配会返回匹配的文本,但不允许我在原始字符串中替换它。有没有一种简单的方法可以做到这一点?
例如,我希望字符串:
"Bob went to the market. Bob went to the fair. Bob went home"
可能会弹出
"Fred went to the market. John went to the fair. Alex went home"