JS:用字符替换空格
JS: Replace whitespace with character
我想弄清楚如何用给定的字符替换所有空格。
我的代码在替换空格时复制字符。
这是练习,到目前为止我的代码:
这个函数接收一个字符串。它还需要一个可选字符,如果未给出,则应默认为下划线 ('_')。
它应该 return 相同的字符串,但所有空格组(空格、制表符和换行符)都被第二个参数中的单个字符实例替换。
function replaceWhitespaceWithCharacter (str, character) {
return str.replace(/\s/g, character);
}
此外,我不知道如何默认下划线。谁能帮帮我好吗?
提前谢谢你。
/\s/g
匹配 一个 空白字符(重复)。要在一行中匹配一个 或更多 个空白字符,请在 \s
之后使用 +
:/\s+/g
.
function replaceWhitespaceWithCharacter (str, character) {
return str.replace(/\s+/g, character);
}
Also, I don't know how to default the underscore
在 ES2015 之前,你会做这样的事情:
function replaceWhitespaceWithCharacter (str, character) {
character = character || "_";
return str.replace(/\s+/g, character);
}
...因为如果不给出 character
将是 undefined
,这是错误的。或者如果你想允许 ""
(这也是假的),那么:
function replaceWhitespaceWithCharacter (str, character) {
character = typeof character === "undefined" ? "_" : character;
return str.replace(/\s+/g, character);
}
从 ES2015 开始,您可以使用默认参数值:
function replaceWhitespaceWithCharacter (str, character = "_") {
return str.replace(/\s+/g, character);
}
要包含多个空格,您应该添加 +
,例如:/\s+/g
所以你的代码应该是:
function replaceWhitespaceWithCharacter (str, character) {
return str.replace(/\s+/g, character);
}
在正则表达式中 one or more
匹配。所以 /\s+/
表示空格至少一个最大任意。然后我检查 character
是否用三元运算符定义。如果不是,那么我将分配 _
。
function replaceWhitespaceWithCharacter (str, character) {
return str.replace(/\s+/g, character?character: '_');
}
这是我的最终解决方案。谢谢你的帮助。
function replaceWhitespaceWithCharacter (str, character) {
if(character){
return str.replace(/\s+/g, character);
}else{
return str.replace(/\s+/g, '_');
}
}
var str = "this is a\ttest\n2";
var str2 = str.replace(/\s+/g,'_');
console.log(str2);
这是用单个下划线替换所有空格。
默认为“_”,插入:
var chr = (typeof chr !== 'undefined') ? chr : '_';
用组中的一个或多个元素(但不是零)替换空白组:
return str.replace(/\s+/g, chr);
所以我们最终得到:
function replaceWhitespaceWithCharacter (str, chr) {
var chr = (typeof chr !== 'undefined') ? chr : '_';
return str.replace(/\s+/g, chr);
}
您还可以使用以下方法扩展字符串对象:
String.prototype.replaceWhitespaceWithCharacter = function replaceWhitespaceWithCharacter(theCharacter)
{
if ((typeof theCharacter !== 'string') || (theCharacter === ''))
{
theCharacter = '_';
}
return this.replace(/\s+/g, theCharacter);
}
我根据字符串类型或字符是否为空字符串检查字符
我想弄清楚如何用给定的字符替换所有空格。 我的代码在替换空格时复制字符。
这是练习,到目前为止我的代码:
这个函数接收一个字符串。它还需要一个可选字符,如果未给出,则应默认为下划线 ('_')。
它应该 return 相同的字符串,但所有空格组(空格、制表符和换行符)都被第二个参数中的单个字符实例替换。
function replaceWhitespaceWithCharacter (str, character) {
return str.replace(/\s/g, character);
}
此外,我不知道如何默认下划线。谁能帮帮我好吗? 提前谢谢你。
/\s/g
匹配 一个 空白字符(重复)。要在一行中匹配一个 或更多 个空白字符,请在 \s
之后使用 +
:/\s+/g
.
function replaceWhitespaceWithCharacter (str, character) {
return str.replace(/\s+/g, character);
}
Also, I don't know how to default the underscore
在 ES2015 之前,你会做这样的事情:
function replaceWhitespaceWithCharacter (str, character) {
character = character || "_";
return str.replace(/\s+/g, character);
}
...因为如果不给出 character
将是 undefined
,这是错误的。或者如果你想允许 ""
(这也是假的),那么:
function replaceWhitespaceWithCharacter (str, character) {
character = typeof character === "undefined" ? "_" : character;
return str.replace(/\s+/g, character);
}
从 ES2015 开始,您可以使用默认参数值:
function replaceWhitespaceWithCharacter (str, character = "_") {
return str.replace(/\s+/g, character);
}
要包含多个空格,您应该添加 +
,例如:/\s+/g
所以你的代码应该是:
function replaceWhitespaceWithCharacter (str, character) {
return str.replace(/\s+/g, character);
}
在正则表达式中 one or more
匹配。所以 /\s+/
表示空格至少一个最大任意。然后我检查 character
是否用三元运算符定义。如果不是,那么我将分配 _
。
function replaceWhitespaceWithCharacter (str, character) {
return str.replace(/\s+/g, character?character: '_');
}
这是我的最终解决方案。谢谢你的帮助。
function replaceWhitespaceWithCharacter (str, character) {
if(character){
return str.replace(/\s+/g, character);
}else{
return str.replace(/\s+/g, '_');
}
}
var str = "this is a\ttest\n2";
var str2 = str.replace(/\s+/g,'_');
console.log(str2);
这是用单个下划线替换所有空格。
默认为“_”,插入:
var chr = (typeof chr !== 'undefined') ? chr : '_';
用组中的一个或多个元素(但不是零)替换空白组:
return str.replace(/\s+/g, chr);
所以我们最终得到:
function replaceWhitespaceWithCharacter (str, chr) {
var chr = (typeof chr !== 'undefined') ? chr : '_';
return str.replace(/\s+/g, chr);
}
您还可以使用以下方法扩展字符串对象:
String.prototype.replaceWhitespaceWithCharacter = function replaceWhitespaceWithCharacter(theCharacter)
{
if ((typeof theCharacter !== 'string') || (theCharacter === ''))
{
theCharacter = '_';
}
return this.replace(/\s+/g, theCharacter);
}
我根据字符串类型或字符是否为空字符串检查字符