Coldfusion字符串替换为ReReplace

时间:2013-11-09 06:27:20

标签: regex string coldfusion coldfusion-9

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

我的替换代码与此类似。

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

1 个答案:

答案 0 :(得分:9)

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

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

这是规格吗?

相关问题