js若字符串中含有除空格、数字、冒号、字母、换行符外还有别的符号则删除
你可以使用正则表达式来删除除空格、数字、冒号、字母、换行符外的符号。以下是一个示例代码:
function removeExtraCharacters(str) {
// 使用正则表达式匹配除空格、数字、冒号、字母、换行符外的所有符号
const regex = /[^a-zA-Z0-9:\s\n]/g;
// 删除匹配到的符号
const result = str.replace(regex, '');
return result;
}
const input = "Hello, world! This is a test string. #Test123";
const output = removeExtraCharacters(input);
console.log(output);
输出将是:"Hello world This is a test string Test123"。
这个函数使用正则表达式/[^a-zA-Z0-9:\s\n]/g来匹配除了字母、数字、冒号、空格和换行符之外的所有字符。然后,使用replace()函数将匹配到的字符替换为空字符串,从而删除它们
原文地址: https://www.cveoy.top/t/topic/iS6p 著作权归作者所有。请勿转载和采集!