【问题标题】:Coldfusion string replace with ReReplaceColdfusion 字符串替换为 ReReplace
【发布时间】:2013-11-21 07:01:09
【问题描述】:

我正在尝试用下划线替换字符串中的空格,以便使用 RegEx 创建 slug。当有一个空间时,它可以正常工作。但是当有两个连续的空格或一个空格后跟一个下划线时,反之亦然(' _' OR '_ '),它被替换为__。我该如何克服呢?即我想要一个下划线而不是双下划线或三重下划线。任何帮助将不胜感激。

我的替换代码与此类似。

rereplace(lCase('this is a sample _string'),'[ ]','_','all')

【问题讨论】:

  • 你没有告诉我们你想要发生什么而是
  • 哦。我想要一个下划线而不是双下划线或三重下划线。
  • 干杯。最好更新问题,而不是简单地添加评论(以供将来参考)。在下面回答。
  • @AdamCameron 编辑了问题哥们...

标签: regex string coldfusion coldfusion-9


【解决方案1】:

根据您修改后的要求,这似乎可以解决问题:

original = "string with_mix _ of  spaces__and_ _underscores__  __to_ _test  with";
updated = reReplace(original, "[ _]+", "_", "all");
writeOutput(updated);

结果:

string_with_mix_of_spaces_and_underscores_to_test_with

这是规范吗?

【讨论】:

  • +1 完美。您能否更新答案以删除字符串末尾的删除空格和下划线?
猜你喜欢
  • 2018-04-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-04-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多