I'm trying to replace some repeated char开发者_C百科acters using regex:
var string = "80--40";
string = string.replace(/-{2}/g,"-");    // result is "80-40"
This replaces two minuses with one, but how could I change the code so that it replaces two or more? I only want one minus symbol to appear between the numbers.
Change it to:
string = string.replace(/-{2,}/g,"-");
Another way is
string = string.replace(/-+/g,"-");
as that replaces any one or more instances of - with only one -.
{2} matches exactly two, + matches one or more.
string = string.replace(/\-+/g, '-');
For more on RegEx, See the MDN documentation
You can specify {x, y} to match any number of repetitions between x and y. You can also leave off the upper or lower bound, so use {2,} instead of {2} to replace any matches that occur at least two times.
 
         
                                         
                                         
                                         
                                        ![Interactive visualization of a graph in python [closed]](https://www.devze.com/res/2023/04-10/09/92d32fe8c0d22fb96bd6f6e8b7d1f457.gif) 
                                         
                                         
                                         
                                         加载中,请稍侯......
 加载中,请稍侯......
      
精彩评论