开发者

Regular Expression to replace multiple hyphens with a single one

开发者 https://www.devze.com 2022-12-15 13:58 出处:网络
I wrote a Regular Expression that generates a url like /abc/deutschland/bbs-tagesfahrten/betz-mode-frotier-开发者_Go百科center-–-tress-teigwaren.html.

I wrote a Regular Expression that generates a url like

/abc/deutschland/bbs-tagesfahrten/betz-mode-frotier-开发者_Go百科center-–-tress-teigwaren.html.

Now I want to replace the repeating dashes with a single one. How can I?


String.replaceAll("--+", "-");


Perhaps simpler that any of the suggestions would be:

s/-{2,}/-/g


Use this:

s/---*/-/g


To replace any repeated dashes in the whole URL:

<cfset InputUrl = "/abc/deutschland/bbs-tagesfahrten/betz-mode-frotier-center-–-tress-teigwaren.html">
<cfset CleanUrl = REReplace(InputUrl, "-+", "-", "ALL")>

To work on the file part only:

<cfset PathPart = REReplace(InputUrl, "(.*/).*", "\1")>
<cfset FilePart = ListLast(InputUrl, "/")>
<cfset CleanUrl = PathPart & REReplace(FilePart, "-+", "-", "ALL")>
0

精彩评论

暂无评论...
验证码 换一张
取 消