在 JavaScript 中用下划线替换空格
How to Replace Spaces with Underscores in JavaScript
使用该String.replaceAll()
方法将 JavaScript 字符串中的所有空格替换为下划线,例如string.replaceAll(' ', '_')
.
该replaceAll
方法返回一个新字符串,其中所有空白字符都替换为下划线。
const str = 'bobby hadz com'; const replaced = str.replaceAll(' ', '_'); console.log(replaced); // 👉️ bobby_hadz_com
String.replaceAll
()
方法返回一个新字符串,其中所有匹配的模式都被提供的替换项替换。
该方法采用以下参数:
姓名 | 描述 |
---|---|
图案 | 要在字符串中查找的模式。可以是字符串或正则表达式。 |
替代品 | 用于通过提供的模式替换子字符串匹配的字符串。 |
该String.replaceAll()
方法返回一个新字符串,其中替换了模式的匹配项。该方法不会更改原始字符串。
字符串在 JavaScript 中是不可变的。
如果您必须经常这样做,请定义一个可重用的函数。
function replaceSpaceWithUnderscore(str) { return str.replaceAll(' ', '_'); } const str = 'bobby hadz com'; const result = replaceSpaceWithUnderscore(str); console.log(result); // 👉️ bobby_hadz_com
该函数接受一个字符串作为参数,将字符串中的所有空格替换为下划线并返回结果。
如果您的字符串有前导和尾随空格,您将获得前导和尾随下划线。
const str = ' bobby hadz com '; const result = str.replaceAll(' ', '_'); console.log(result); // 👉️ _bobby_hadz_com_
您可以使用该trim()
方法在调用之前删除前导和尾随空格replaceAll()
。
const str = ' bobby hadz com '; const result = str.trim().replaceAll(' ', '_'); console.log(result); // 👉️ bobby_hadz_com
在 JavaScript 中使用下划线替换空格String.replace()
或者,您可以使用该String.replace()
方法。
const str = 'bobby hadz com'; const strUnderscores = str.replace(/ /g, '_'); console.log(strUnderscores); // 👉️ bobby_hadz_com
如果您需要用单个下划线替换一个或多个空格,请使用加号
+
。
const str = 'bobby hadz com'; const strUnderscores = str.replace(/ +/g, '_'); console.log(strUnderscores); // 👉️ bobby_hadz_com
加号+
与前面的项目(空格)匹配一次或多次。
String.replace
()
方法返回一个新字符串,其中一个、一些或所有正则表达式的匹配项被替换为提供的替换项。
该方法采用以下参数:
姓名 | 描述 |
---|---|
图案 | 要在字符串中查找的模式。可以是字符串或正则表达式。 |
替代品 | 用于通过提供的模式替换子字符串匹配的字符串。 |
我们传递给该方法的第一个参数replace()
是一个正则表达式。
正斜杠标记/ /
正则表达式的开始和结束。
在我们的正则表达式中,我们有一个空格。
const str = 'bobby hadz com'; const strUnderscores = str.replace(/ /g, '_'); console.log(strUnderscores); // 👉️ bobby_hadz_com
我们g
向正则表达式添加了(全局)标志,这使得正则表达式匹配字符串中的所有空格,而不仅仅是第一次出现。
用下划线替换第一次出现的空格
如果您要删除该g
标志,则只有第一次出现的空格会被替换为下划线。
const str = 'bobby hadz com'; const strUnderscores = str.replace(/ /, '_'); console.log(strUnderscores); // 👉️ bobby_hadz com
代码示例不使用g
标志,因此只有字符串中的第一个空格被替换为下划线。
用下划线替换所有空白字符
如果需要用下划线替换所有空白字符,请使用\s
特殊字符。
const str = 'bobby\nhadz\tcom abc'; const strUnderscores = str.replace(/\s/g, '_'); console.log(strUnderscores); // 👉️ bobby_hadz_com_abc
特殊\s
字符匹配空格、制表符和换行符。
使用split()
and将所有空格替换为下划线join()
这是一个两步过程:
- 使用该
split()
方法
在每个空格上拆分字符串。 - 使用该
join()
方法连接带有下划线分隔符的数组。
const str = 'bobby hadz com'; const result = str.split(' ').join('_'); console.log(result); // 👉️ bobby_hadz_com
String.split ()
方法接受一个分隔符,并在每次出现所提供的分隔符时将字符串拆分为一个数组。
该String.split()
方法采用以下 2 个参数:
姓名 | 描述 |
---|---|
分隔器 | 描述每个拆分应该发生的位置的模式。 |
限制 | 一个整数,用于指定要包含在数组中的子字符串数的限制。 |
我们在每次出现空格时拆分字符串。
const str = 'bobby hadz com'; // 👇️ [ 'bobby', 'hadz', 'com' ] console.log(str.split(' '));
最后一步是使用下划线分隔符连接数组。
Array.join ()方法使用分隔符连接数组中的所有元素。
该方法采用的唯一参数Array.join()
是separator
– 用于分隔数组元素的字符串。
const str = 'bobby hadz com'; const result = str.split(' ').join('_'); console.log(result); // 👉️ bobby_hadz_com
# 额外资源
您可以通过查看以下教程来了解有关相关主题的更多信息: