js使用正则表达式去掉文本中的空格、标点及符号,仅仅保留中文、英文、数字,input textarea排除标点符号空格等计算长度

js使用正则表达式去掉文本中的空格、标点及符号,仅仅保留中文、英文、数字,input textarea排除标点符号空格等计算长度

const countWords = (text) => {

  const regex = /[^\u4e00-\u9fa5a-zA-Z0-9]/g;
    // 使用replace方法替换匹配到的字符为空字符串
    return text.replace(regex, '').length;

}

console.log('len',countWords(formData.value.logContent))

 

http://www.feiyunjs.com/3710.html

 


function retainChineseEnglishAndNumbers(text) {
    // 使用正则表达式匹配所有中文字符、英文字符和数字
    const regex = /[^\u4e00-\u9fa5a-zA-Z0-9]/g;
    // 使用replace方法替换匹配到的字符为空字符串
    return text.replace(regex, '');
}

// 示例用法
const inputText = "Hello, 世界! This is a test... How are you? 你好,世界!";
const cleanedText = retainChineseEnglishAndNumbers(inputText);
console.log(cleanedText); // 输出: "Hello世界ThisisatestHowareyou你好世界"

 

posted @ 2024-11-27 09:11  Shimily  阅读(119)  评论(0)    收藏  举报