[英]How to remove spaces and special characters from string?
I have a function that returns true if a character is a form of punctuation and I'm trying to write another function that accepts a string and removes the spaces and punctuation marks while calling the first function. 我有一个函数,如果字符是标点符号的形式,则返回true,我正在尝试编写另一个函数,该函数接受字符串并在调用第一个函数时删除空格和标点符号。 I got most of it I think.
我想我大部分。 But now I'm stuck.
但是现在我被卡住了。 Any help is appreciated.
任何帮助表示赞赏。
var isPunct = function(ch) {
if (ch = ch.match(/[,.!?;:'-]/g))
return true
else
return false
}
//B
var compress = function(s) {
var result = "";
//loop to traverse s
for (var i = 0; i < s.length; i++) {
if (!(isPunct(ch));
//(isPunct(s.charAt(i) || s.charAt(i) == " "));
//do nothing
else
result = result + !compress(i)
}
return result
}
Some issues: 一些问题:
The inner condition should in fact be the opposite: you want to do nothing when it is a punctuation character, ie you don't want to add it to the result. 内部条件实际上应该相反:当它是标点符号时,您不希望执行任何操作,即,您不想将其添加到结果中。 Only in the other case you want to do that.
只有在其他情况下,您才想这样做。
The call !compress(i)
is wrong: first of all that function expects a string, not an index, and it returns a string, not a boolean (so to perform !
on it). 调用
!compress(i)
是错误的:首先,该函数需要一个字符串,而不是索引,并且它返回一个字符串,而不是布尔值(因此要对它执行!
)。 It seems like you want to call your function recursively, and although that is an option, you are also iterating over the string. 似乎您想递归地调用函数,尽管这是一个选择,但您也正在对字符串进行迭代。 You should do one of the two: recursion or iteration.
您应该执行以下两项操作之一:递归或迭代。
You reference a variable ch
in the compress
function which you have not defined there. 您在
compress
函数中引用了尚未在其中定义的变量ch
。
So, if you want to write compress
the iteration way, change your code as follows: 因此,如果要编写
compress
迭代方式,请按如下所示更改代码:
var compress = function(s) {
var result = "", ch; // define ch.
//loop to traverse s
for (var i = 0; i < s.length; i++) {
ch = s[i]; // initialise ch.
if (!isPunct(ch)) result = result + ch; // only add when not punctuation
}
return result;
}
If on the other hand you want to keep your recursive call to compress
, then you should do away with your for
loop: 另一方面,如果您想保留递归调用
compress
,则应该取消for
循环:
var compress = function(s) {
var result = "", ch, rest;
if (s.length == 0) return '';
result = compress(s.substr(1)); // recursive call
ch = s[0];
if (!isPunct(ch)) result = ch + result;
return result;
}
The function isPunct
also has a strange thing happening: it assigns a boolean value to ch
in the if
expression. 函数
isPunct
也发生了一件奇怪的事情:它在if
表达式中为ch
分配一个布尔值。 This does not make your function malfunction, but that assignment serves no purpose: the match
method already returns the boolean you need for your if
condition. 这不会使您的功能出现故障,但是该分配没有任何作用:
match
方法已经返回了if
条件所需的布尔值。
It is also not really nice-looking to first evaluate a boolean expression in an if
to then return that same value in the form of false
and true
. 首先在
if
对布尔表达式求值,然后以false
和true
的形式返回相同的值,看起来也不是很好。 This you can do by just returning the evaluated expression itself: 您可以通过只返回评估表达式本身来做到这一点:
var isPunct = function(ch) {
return ch.match(/[,.!?;:'-]/g);
}
On a final note, you don't really need the isPunct
function if you only use it in compress
. 最后一点,如果仅在
compress
使用isPunct
函数,则实际上并不需要它。 The whole logic can be performed in one function only, like this: 整个逻辑只能在一个功能中执行,如下所示:
let compress = s => s.replace(/[,.!?;:'-]/g,''); // Demo: console.log(compress('a,b,c')); // abc
If you prefer to keep isPunct
and don't want to repeat the regular expression elsewhere, then you can do the replace like this: 如果您希望保留
isPunct
且不想在其他地方重复正则表达式,则可以执行以下替换:
let isPunct = ch => ch.match(/[,.!?;:'-]/g); let compress = s => Array.from(s).filter(ch => !isPunct(ch)).join(''); // Demo: console.log(compress('a,b,c')); // abc
Note how the use of ES6 arrow functions and ES5 Array methods makes the code quite lean. 请注意,使用ES6箭头功能和ES5数组方法如何使代码相当精简。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.