简体   繁体   English

如何从作为参数的字符串中删除所有特殊字符?

[英]How to remove all special characters from the character string that comes as a parameter?

I'm practicing Javascript logic exercises to improve in this language and prepare for future interviews and I find myself stuck on something that may seem basic, but I can't find the way around it, I don't know if someone could explain to me how to solve this problem.我正在练习 Javascript 逻辑练习以提高这种语言并为未来的面试做准备,我发现自己陷入了一些看似基本的事情,但我找不到解决方法,我不知道是否有人可以解释我如何解决这个问题。

The statement says that: The purpose of the FormatString function is to remove all special characters from the character string that comes as a parameter.该声明说: FormatString function 的目的是从作为参数的字符串中删除所有特殊字符。 Only the 26 letters of the English alphabet, numbers 0-9, spaces, and hyphens are allowed.仅允许使用 26 个英文字母、数字 0-9、空格和连字符。

The program is as follows:程序如下:

function FormatString(sentence) {
let result = []; 

sentence = sentence.toUpperCase(); 

let i = 0; 
let j = 0;

while (i < sentence.lenght){
    if(
        (sentence.charCodeAt(i) >= 65 && sentence.charCodeAt(i) <=90) || (sentence.charCodeAt(i) >= 48 && sentence.charCodeAt(j) <= 57) || sentence.charCodeAt(i) == 32 ||
        sentence.charCodeAt(i) == 45
    ) {
        sentence[j] = result[i];
        j+=1;
    }
    i+=1;
}
return result.join(""); 
}

There are supposed to be errors in the exercise, but I can't identify them, I would like to make the code work, sorry if it's something very basic, but I'm still learning.练习中应该有错误,但我无法识别它们,我想让代码工作,对不起,如果它是非常基本的东西,但我仍在学习。 Thank you.谢谢你。

This is the easiest (and most elegant) method to accomplish what you need.这是完成您需要的最简单(也是最优雅)的方法。 Match the items and joined the matched array into a string:匹配项目并将匹配的数组加入一个字符串:

function FormatString(sentence) {
    return sentence.match(/[a-z]|[0-9]|-|\s/gi).join('')
}

To Test:去测试:

console.log(FormatString('%this is 1212-33 QB)_'))

Use replace() method from String and regular expression.使用 String 和正则表达式中的replace()方法。

Basically, you want to remove everything that is not a-zA-Z0-9 -, so:基本上,你想删除所有不是a-zA-Z0-9 - 的东西,所以:

const formatString = (sentence) => sentence.replace(/[^a-zA-Z0-9 -]*/g, '');

More info on the official documentation of RegExp有关RegExp官方文档的更多信息

For replace method: https://developer.mozilla.org/fr/docs/Web/JavaScript/Reference/Global_Objects/String/replace对于replace方法: https://developer.mozilla.org/fr/docs/Web/JavaScript/Reference/Global_Objects/String/replace

First, I don't know if it is part of your exercise or not but you typed sentence.lenght, it is length首先,我不知道这是否是你练习的一部分,但你输入了 sentence.lenght,它是长度

second: your code is actually uppercasing the sentence so it would return uppered sentence第二:你的代码实际上是大写的句子,所以它会返回大写的句子

you could add你可以添加

(sentence.charCodeAt(i) >= 97 && sentence.charCodeAt(i) <= 122)

to check for "az" letters, don't forget to remove sentence.toUpperCase()要检查“az”字母,不要忘记删除 sentence.toUpperCase()

You have misspelled length and its causing the output you're getting.您拼写错误的length及其导致您得到的 output 。 And if you want to continue the approach in the example, you have to implement all the other character codes also.如果您想继续示例中的方法,您还必须实现所有其他字符代码。

A shorter way would be:更短的方法是:

const formatString = (sentence) => sentence.replace(/[^a-z\d\s\-]+/igm, '');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM