简体   繁体   English

3 While循环成一个循环?

[英]3 While Loops into a Single Loop?

I have to remove the commas, periods, and hyphens from an HTML text value. 我必须从HTML文本值中删除逗号,句点和连字符。 I do not want to write all 3 of these while loops, instead I only want one loop (any) to do all of this. 我不想编写所有这3个while循环,而是只希望一个循环(任何一个)来完成所有这一切。

I already tried a while with multiple && and if else nested inside but i would always only just get the commas removed. 我已经尝试了一段时间,其中有多个&&,如果嵌套在其他位置,但我总是只会删除逗号。

while(beg.indexOf(',') > -1)
{
    beg = beg.replace(',','');
    document.twocities.begins.value= beg;
}

while(beg.indexOf('-') > -1)
{
    beg = beg.replace('-','');
    document.twocities.begins.value= beg;
}
while(beg.indexOf('.') > -1)
{
    beg= beg.replace('.','');
    document.twocities.begins.value= beg;

}

You can do all this without loops by using regex. 您可以使用正则表达式来完成所有这些操作而无需循环。 Here is an example of removing all those characters using a single regex: 这是使用单个正则表达式删除所有这些字符的示例:

 let str = "abc,de.fg,hij,1-2,34.56.7890" str = str.replace(/[,.-]/g, "") console.log(str) 

Use regex like below. 使用正则表达式如下。

 let example = "This- is a,,., string.,"; console.log(example.replace(/[-.,]+/g, "")); 

A single call to the replace function and using a regular expression suffice: 一次调用replace函数并使用正则表达式就足够了:

document.twocities.begins.value = beg = beg.replace(/[,.-]/g, "");

Regular expressions are a pattern matching language. 正则表达式是一种模式匹配语言。 The pattern employed here basically says "every occurrence of one of the characters . , , , - )". 这里采用的模式基本上说“人物之一的每次出现.,- )”。 Note that the slash / delimits the pattern while the suffix consists of flags controlling the matching process - in this case it is g (global) telling the engine to replace each occurrence ( as opposed to the first only without the flag ). 请注意,斜杠/限制了模式,而后缀由控制匹配过程的标志组成-在这种情况下,它是g (全局),告诉引擎替换每个匹配项(与第一次不带该标志的情况相反)。

This site provides lots of info about regular expressions, their use in programming and implementations in different programming environments. 该站点提供了大量有关正则表达式,它们在编程中的用法以及在不同编程环境中的实现的信息。

There are several online sites to test actual regular expression and what they match (including explanations), eg. 有多个在线站点可以测试实际的正则表达式及其匹配项(包括说明),例如。 Regex 101 . 正则表达式101

Even more details ... ;): You may use the .replace function with a string as the first argument (as you did in your code sample). 甚至更多的细节...;):您可以将.replace函数与字符串一起用作第一个参数(就像在代码示例中所做的那样)。 However, only the first occurrence of the string searched for will be replaced - thus you would have to resort to loops. 但是,只有第一次出现的搜索字符串将被替换-因此,您将不得不求助于循环。 Specs of the .replace function (and of JS in general) can be found here . .replace函数(通常是JS)的规范可以在这里找到。

No loops are necessary for this in the first place. 首先,不需要循环。

You can replace characters in a string with String.replace() and you can determine which characters and patterns to replace using regular expressions . 您可以使用String.replace()替换字符串中的字符,并可以使用正则表达式确定要替换的字符和模式。

 let sampleString = "This, is. a - test - - of, the, code. "; console.log(sampleString.replace(/[,-.]/g, "")); 

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

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