我有一个包含特殊字符的字符串。但是我必须将该字符串转换为不含任何特殊字符的字符串,所以我使用了Base64。但在Base64中,我们使用等号(=)符号,这是一个特殊字符。但我想将该字符串转换为仅具有字母数字字符的字符串。同时,我不能删除特殊字符,只需替换所有特殊字符以保持两个不同字符串之间的唯一性。如何实现这一点?哪种编码可以帮助我实现这个目标?
Commons codec 提供了一个URL安全版本的base64,它用 - 和 _ 替代 + 和 / 字符。
http://commons.apache.org/codec/apidocs/org/apache/commons/codec/binary/Base64.html#encodeBase64URLSafe(byte[])
// This will remove all special characters except space.
var cleaned = stringToReplace.replace(/[^\w\s]/gm, '')
在上述正则表达式中添加任何特殊字符将跳过该字符。
// This will remove all special characters except space and period.
var cleaned = stringToReplace.replace(/[^\w\s.]/gm, '')
const regex = /[^\w\s]/gm;
const str = `This is a text with many special characters.
Hello, user, your password is 543#!\$32=!`;
const subst = ``;
// The substituted value will be contained in the result variable
const result = str.replace(regex, subst);
console.log('Substitution result: ', result);
[^\w\s]/gm
Match a single character not present in the list below [^\w\s]
\w matches any word character (equivalent to [a-zA-Z0-9_])
\s matches any whitespace character (equivalent to [\r\n\t\f\v \u00a0\u1680\u2000-\u200a\u2028\u2029\u202f\u205f\u3000\ufeff])
Global pattern flags
g modifier: global. All matches (don't return after first match)
m modifier: multi line. Causes ^ and $ to match the begin/end of each line (not only begin/end of string)
This is my special sentence with a 0.
编码为:
This020is020my020special020sentence020with020a02000002e