我试图用*替换连字符、空格和问号,在ColdFusion中使用正则表达式。看上去:
ReReplace( txt, "-?\s+\?*","*", "All" )但它并没有取代字符串末尾的问号。有什么帮助吗?
发布于 2016-03-07 11:30:14
为了提高代码的可读性,我会这样写:
<cfscript>
txt = "Hello! testing 1-2-3. How are you?";
foo = ReReplace(txt, "[- ?]","*", "All");
writeDump(foo);
</cfscript>它将返回:
Hello!*testing*1*2*3*How*are*you*范围内的字符(方括号)不需要转义,这使我更容易阅读(无论如何!)。值得注意的是,-是一个范围内的特殊字符,所以我把它作为第一个字符。
如果使用[ -?],正则表达式将匹配空格和?之间的任何字符并返回:
Hello**testing*******How*are*you*https://stackoverflow.com/questions/35842164
复制相似问题