jQuery 替换字符串空格:使用 replace() 函数
在 jQuery 中,可以使用'replace()'函数来替换字符串中的空格。以下是一个示例:
var str = 'Hello World';
var newStr = str.replace(/\s/g, '-');
console.log(newStr); // 输出 'Hello-World'
在上述示例中,我们使用正则表达式'/\s/g'来匹配字符串中的所有空格,并使用'replace()'函数将其替换为'-'. 最后,将替换后的字符串输出到控制台。
原文地址: https://www.cveoy.top/t/topic/qiG7 著作权归作者所有。请勿转载和采集!