最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

Javascript regex match and replace percent symbol - Stack Overflow

matteradmin9PV0评论

sorry for my english

I am trying replace everything that is not %d, %m or %Y from a string, I have been trying but I do not get it, here is my best attempt code:

var old_string = "%l %d de %M de %Y (Semana %W)";               
string = old_string.replace(/[^(%d|%m|%Y)]/g, " ");             
alert(old_string + " <----> " + string);

Some help? What am I doing wrong?

sorry for my english

I am trying replace everything that is not %d, %m or %Y from a string, I have been trying but I do not get it, here is my best attempt code:

var old_string = "%l %d de %M de %Y (Semana %W)";               
string = old_string.replace(/[^(%d|%m|%Y)]/g, " ");             
alert(old_string + " <----> " + string);

Some help? What am I doing wrong?

Share Improve this question asked Jan 9, 2012 at 18:01 el_quickel_quick 4,75611 gold badges47 silver badges53 bronze badges 3
  • 1 For one thing don't name a string variable string that is a javascript keyword... – Naftali Commented Jan 9, 2012 at 18:02
  • 1 What should the example string look like after the replacements? Do you want an additional space in the string for each word? Should the parentheses remain? – murgatroid99 Commented Jan 9, 2012 at 18:16
  • 2 string is certainly not a keyword in JavaScript. Although it's not a very good choice for a variable, there is nothing wrong with it for this example. – pimvdb Commented Jan 9, 2012 at 18:21
Add a ment  | 

3 Answers 3

Reset to default 3

If I understand correctly, you want to the substrings %d, %m and %Y from your string. I assume you want one space between each match, and want to retain the original order of occurrence.

You can do this using String.match() and Array.join(), like so:

var old_string = "%l %d de %m de %Y (Semana %W)";
var matches = old_string.match(/%[dmY]/g);
var new_string = matches.join(" ");

alert(new_string); // "%d %m %Y"

Edit: here is a working demo: http://jsfiddle/PPvG/Gv3rX/1/

Edit (2): I realised the regular expression could be simplified further.

In your code you are using character classes incorrectly. Your regular expression will match any single character that is not one of (%d|mY). Instead you probably want to use negative lookahead to check that the string you are matching is not one of the specified strings. The following regular expression should do it:

/(?!(.*?)%(d|m|Y).*).*/

This will replace strings of any length that do not contain the specified strings with a single space.

If I understood correctly, your resulting string would be %d de de %Y (Semana), which makes no sense to me since I'm a Spanish user. But here is an alternative to your regex sugestion.

var old_string = "%l %d de %M de %Y (Semana %W)";
var new_string = "";

var i = 0;
while (i < old_string.length) {
    var char = old_string[i];

    new_string += char; 
    if (char == "%") {
        ++i;

        var next_char = old_string[i];

        switch (next_char) {
            case "d":
            case "m":
            case "Y":
                new_string += next_char;
                break;
            default:
                new_string = new_string.substring(0, new_string.length - 2);
        }
    }

    ++i;
}


alert(new_string);
Post a comment

comment list (0)

  1. No comments so far