Coldfusion字符串替换为ReReplace

Mar*_*rai 4 regex string coldfusion coldfusion-9

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

我的替换代码与此类似.

rereplace(lCase('this is a sample _string'),'[ ]','_','all')
Run Code Online (Sandbox Code Playgroud)

Ada*_*ron 10

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

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

结果是:

string_with_mix_of_spaces_and_underscores_to_test_with
Run Code Online (Sandbox Code Playgroud)

这是规格吗?