-
-
Notifications
You must be signed in to change notification settings - Fork 3.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[js] 第2天 写一个方法去掉字符串中的空格 #6
Comments
欢迎大家展开多种不同的方法 |
|
var trim = function(str){ |
|
const str = ' s t r '
const POSITION = Object.freeze({
left: Symbol(),
right: Symbol(),
both: Symbol(),
center: Symbol(),
all: Symbol(),
})
function trim(str, position = POSITION.both) {
if (!!POSITION[position]) throw new Error('unexpected position value')
switch(position) {
case(POSITION.left):
str = str.replace(/^\s+/, '')
break;
case(POSITION.right):
str = str.replace(/\s+$/, '')
break;
case(POSITION.both):
str = str.replace(/^\s+/, '').replace(/\s+$/, '')
break;
case(POSITION.center):
while (str.match(/\w\s+\w/)) {
str = str.replace(/(\w)(\s+)(\w)/, `$1$3`)
}
break;
case(POSITION.all):
str = str.replace(/\s/g, '')
break;
default:
}
return str
}
const result = trim(str)
console.log(`|${result}|`) // |s t r| |
function trimStr(str, type) { const regObj = { left: /^\s+/, middle: /(^\s+)(\S)|\s+(\S)/g, right: /\s+$/, both: /(^\s+)|(\s+$)/g, all: /\s+/g }; const reg = type && regObj[type] ? regObj[type] : regObj.both; const replaceStr = type === 'middle' ? (m, $1, $2, $3) => $1 ? m : $3 : ''; return str.replace(reg, replaceStr); } trimStr(' aa bb cc d d ee ','middle'); |
|
var str = ' 1 2 3445 6 ';
console.log(str.split(' ').join('')) // 输出"1234456" 这样不是很简单吗 @haizhilin2013 |
function noSpace(str){ |
" 123 56 ".replace(/\s+/g, "") |
function trim(str) {
return str.split(' ').join('');
}
var result = trim(' hello world, I am keke. ');
console.log(result); // helloworld,Iamkeke. |
str.trim() |
|
|
function removeSpace (str, type) {
} |
|
正则不是很熟,感觉中间的空格也能用正则去掉 const trimString = ({str = "", position = "both"}) => {
if (!str) {
return str;
}
const removePos = {
left: () => str.replace(/^\s+/, ""),
right: () => str.replace(/\s+$/, ""),
both: () => str.replace(/(^\s+)|(\s+$)/g, ""),
// 这个方法在字符串中间有多个空格时会有问题
// middle: () =>
// str
// .split(" ")
// .map((item) => (item ? item : " "))
// .join(""),
// 下面这种正则更优雅
// middle: () => {
// let result = str;
// while (/\w+\s+\w+/.test(result)) {
// result = result.replace(/(\w+)\s+(\w+)/, '$1$2');
// }
// return result;
// },
// 一行正则
// middle: () => str.replace(/\b\s*\b/g,''),
// 普通方法
middle: () => {
const leftSpace = str.match(/^\s+/)[0];
const rightSpace = str.match(/\s+$/)[0];
return leftSpace + str.split(" ").join("") + rightSpace;
},
all: () => str.split(" ").join("")
};
return removePos[position]();
};
const a = " 12a b cde fff ";
console.log("trim left:", trimString({str: a, position: "left"}));
console.log("trim right:", trimString({str: a, position: "right"}));
console.log("trim middle", trimString({str: a, position: "middle"}));
console.log("trim both:", trimString({str: a}));
console.log("trim all:", trimString({str: a, position: "all"})); |
不太明白为什么要分左中右,正着是贪婪的 function trim (str) {
let reg = /\s+/g
return str.replace(reg, '')
} |
这个如果遇到连续的空格呢 |
全角空格还需要匹配“\u3000” |
@boboyu 格式化下代码吧,用markdown的语法 |
连续的也没有问题啊 |
let str = '21 sdf asdf 123 ' |
function trim(str, types = ['start', 'end', 'middle']) {
types.forEach(type => {
switch (type) {
case 'start':
str = str.replace(/^\s*/, '')
break
case 'end':
str = str.replace(/\s*$/, '')
break
case 'middle':
while (str.match(/(\S)\s+(\S)/)) {
str = str.replace(/(\S)\s+(\S)/g, '$1$2')
}
break
default:
break
}
})
return str
} |
var removeSpace1 = (str, type) => {
if (!str) return
switch(type){
case 'ALL':
return str.replace(/\s/g, '')
case 'TRIM_HEAD_AND_TAIL':
return str.replace(/^\s*|\s*$/g, '')
case 'TRIM_HEAD':
return str.replace(/^\s*/g, '')
case 'TRIM_TAIL':
return str.replace(/\s*$/g, '')
default:
return str
}
} |
|
注意审题啊,要求的是去掉空格,不是去掉空白字符
|
这个是删去两端的空格,而不会删去字符中间的空格:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/Trim |
|
|
str.replace(/\s*/g,"") |
|
function deleteSpace(str){ |
let str = ' a s ddd ' |
function strFormat(str) { |
function trim(str) { |
function trim(str) { |
var a=" a";
var b = "b ";
var c = " d ";
var d = " " ;
var s = "" ;
s= s ;
function quchu( s ) {
for(let i = 0 ;i <s.length ;i ++ ) {
if (s[i]==" ") {
}
else {
console.log(s[i]);
}
}
}
quchu(s); |
let b="<a> 测试1</a><a> 测试2</a><a> 测试3 </a>"
console.log(b.replace(/\s*/g, ''))
输出:
<a>测试1</a><a>测试2</a><a>测试3</a> |
// 写一个方法去掉字符串中的空格,要求传入不同的类型分别能去掉前、后、前后、中间的空格 var str1 = ' aa bbbd ccc de ' |
str.replaceAll(' ',''); |
let str = ' 1 2 3 4 5 6 7 8 9 1 0 '; |
const removeSpace = (str) => {//去除所有空格
|
您好,邮件已收到,我会尽快回复。
|
let str = "hello world"; |
function clearSpaceFromString (type, str) {
let newStr = ''
switch (type) {
case 'all':
newStr = str.replaceAll(' ', '')
break;
case 'both':
newStr = str.trim()
break;
case 'start':
str[0] === ' ' ? newStr = str.replace(' ', '') : newStr = str
break;
case 'end':
str[str.length - 1] === ' ' ? newStr = str.replace(/\s*$/, '') : newStr = str
break;
default:
newStr = str
break;
}
return newStr
} |
|
|
const str = " ab cd e f 4g h "; |
您好,邮件已收到,我会尽快回复。
|
function removeStringSpace(string) { |
您好,邮件已收到,我会尽快回复。
|
你的邮件我已收到,我会尽快处理
|
1、去除两端空格 |
function trimStr (str, type) {
const regObj = {
head: /^\s+/,
end: /\s+$/,
middle: /(^\s+)(\S)|\s+(\S)/g,
both: /^\s+|\s+$/g,
all: /\s+/g,
}
const reg = type ? regObj[type] : regObj.all
const replaceStr = type === 'middle' ? ((m, $1, $2, $3) => $1 ? m : $3) : ''
return str.replace(reg, replaceStr)
}
var str = ' aa ass cc sa '
console.log(trimStr(str, 'middle')) |
写一个方法去掉字符串中的空格,要求传入不同的类型分别能去掉前、后、前后、中间的空格
The text was updated successfully, but these errors were encountered: