网站优化

网站优化

Products

当前位置:首页 > 网站优化 >

学习去除前后空格,提升代码整洁度!

GG网络技术分享 2025-11-13 03:07 1


javascript // 用正则表达式去除字符串的前后空格 const str = ' hello world! '; const trimmedStr = str.replace; console.log; // 输出: "hello world!"

// 用trim方法去除字符串的前后空格 const strWithTrim = ' hello world! '; const trimmedStrWithTrim = strWithTrim.trim; console.log; // 输出: "hello world!"

// 用split和join方法去除字符串中的优良几个连续空格 const strWithExtraSpaces = ' hello world! '; const noExtraSpaceStr = strWithExtraSpaces.replace.trim; console.log; // 输出: "hello world!"

// 用split方法去除字符串的前后空格 const strForSplit = ' hello world! '; const splitStr = strForSplit.split; const trimmedStrWithSplit = splitStr.filter.join; console.log; // 输出: "hello world!"

标签:

提交需求或反馈

Demand feedback