How to remove more than one whitespace character from HTML?

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-02 00:23:45

You could use regex to replace any cases of multiple whitespace characters with a single space by looping over the result until no more multiple whitespace occurances exist:

lastTry = "<p>   lots of space    </p>";
nextTry = rereplace(lastTry,"\s\s", " ", "all");
while(nextTry != lastTry) {
  lastTry = nextTry;
  nextTry = REReplace(lastTry,"\s\s", " ", "all");
}

Tested working in CF10.

if you don't want to do it thru code out of total lazyness

=> http://jsbeautifier.org/

if you want to do it by code then a regex would be another option

This should do it:

<cfscript>
  string function stripCRLFAndMultipleSpaces(required string theString) {
    local.result = trim(rereplace(trim(arguments.theString), "([#Chr(09)#-#Chr(30)#])", " ", "all"));
    local.result = trim(rereplace(local.result, "\s{2,}", " ", "all"));
    return local.result;
  }
</cfscript>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!