0

我们目前一直在使用我们从 cflib.org 使用的 UDF,虽然它是一个很棒的 UDF,可以将字符串更改为美观,但我们似乎无法弄清楚如何使其允许尾随“。” 如果输入,则在字符串的末尾。

<cffunction name="friendlyURL" output="false" access="public" returntype="string" hint="returns a URL safe string">
<cfargument name="string" default="" type="string">
<cfscript>
var returnString = arguments.string;
var InvalidChars = "à,ô,d,?,ë,š,o,ß,a,r,?,n,a,k,s,?,n,l,h,?,ó,ú,e,é,ç,?,c,õ,?,ø,g,t,?,e,c,s,î,u,c,e,w,?,u,c,ö,è,y,a,l,u,u,s,g,l,ƒ,ž,?,?,å,ì,ï,?,t,r,ä,í,r,ê,ü,ò,e,ñ,n,h,g,d,j,ÿ,u,u,u,t,ý,o,â,l,?,z,i,ã,g,?,o,i,ù,i,z,á,û,þ,ð,æ,µ,e,À,Ô,D,?,Ë,Š,O,A,R,?,N,A,K,S,?,N,L,H,?,Ó,Ú,E,É,Ç,?,C,Õ,?,Ø,G,T,?,E,C,S,Î,U,C,E,W,?,U,C,Ö,È,Y,A,L,U,U,S,G,L,ƒ,Ž,?,?,Å,Ì,Ï,?,T,R,Ä,Í,R,Ê,Ü,Ò,E,Ñ,N,H,G,Ð,J,Ÿ,U,U,U,T,Ý,O,Â,L,?,Z,I,Ã,G,?,O,I,Ù,I,Z,Á,Û,Þ,Ð,Æ,?,E";
var ValidChars   = "a,o,d,f,e,s,o,ss,a,r,t,n,a,k,s,y,n,l,h,p,o,u,e,e,c,w,c,o,s,o,g,t,s,e,c,s,i,u,c,e,w,t,u,c,oe,e,y,a,l,u,u,s,g,l,f,z,w,b,a,i,i,d,t,r,ae,i,r,e,ue,o,e,n,n,h,g,d,j,y,u,u,u,t,y,o,a,l,w,z,i,a,g,m,o,i,u,i,z,a,u,th,dh,ae,u,e,A,O,D,F,E,S,O,A,R,T,N,A,K,S,Y,N,L,H,P,O,U,E,E,C,W,C,O,S,O,G,T,S,E,C,S,I,U,C,E,W,T,U,C,Oe,E,Y,A,L,U,U,S,G,L,F,Z,W,B,A,I,I,D,T,R,Ae,I,R,E,Ue,O,E,N,N,H,G,D,J,Y,U,U,U,T,Y,O,A,L,W,Z,I,A,G,M,O,I,U,I,Z,A,U,TH,Dh,Ae,U,E";
// trim the string
returnString = Trim(returnString);
returnString = StripCR(returnString);
// replace known characters with the corresponding safe characters
returnString = ReplaceList(returnString,InvalidChars,ValidChars);
// replace unknown characters in the x00-x7F-range with x's
returnString = returnString.ReplaceAll('[^\x00-\x7F]','x');
// Replace one or many comma with a dash
returnString = returnString.ReplaceAll(',+', '-');
// Other substitutions
returnString = Replace(returnString, "%", "percent","ALL");
returnString = Replace(returnString, "&amp;", " and ","ALL");
returnString = Replace(returnString, "&", " and ","ALL");
returnString = returnString.ReplaceAll('[:,/]', '-');
// Replace one or more whitespace characters with a dash
returnString = returnString.ReplaceAll('[\s]+', '-');
// And everything else simply has to go
returnString = returnString.ReplaceAll('[^A-Za-z0-9\/-]','');
// finally replace multiple dash characters with just one
returnString = returnString.ReplaceAll('-+','-');
// we're done
return returnString;
</cfscript>
</cffunction>

我的正则表达式,不是很好,我已经花了几个小时修补它,但似乎仍然无法弄清楚如何让它有一个尾随的“。” 如果输入一个。

4

2 回答 2

4
// And everything else simply has to go
returnString = returnString.ReplaceAll('[^A-Za-z0-9\/-]','');

这将用空字符串替换不属于集合的任何字符。尝试.在集合末尾添加字符:

 returnString = returnString.ReplaceAll('[^A-Za-z0-9.-]','');

注意:正如@PeterBoughton 所指出的,已经/被替换为-,所以它不必是字符集的一部分。

现在,这将允许.字符串中间的字符,而您只想在末尾允许它们,因此您必须替换任何.不在末尾的字符。

returnString = returnString.replaceAll('\.(?!$)', '');

否则,您总是可以尝试匹配.后跟任何字符的 a 并使用反向引用将两者替换为该字符。

returnString = returnString.replaceAll('\.(.)', '\1');
于 2013-10-22T01:47:26.633 回答
1

这条线

    // And everything else simply has to go
    returnString = returnString.ReplaceAll('[^A-Za-z0-9\/-]','');

删除所有不是A-Z, a-z, 0-9, /, 或-.

[]括号括起可能匹配的字符列表,^表示“不”)。

您应该能够添加.到列表中:

    // And everything else simply has to go
    returnString = returnString.ReplaceAll('[^A-Za-z0-9\/.-]','');

(正如@Leigh 在下面提到的那样,.必须在决赛之前,-否则它将被视为一个范围。)

于 2013-10-22T01:39:31.410 回答