RegExp:在任何数字之前插入位数

RegExp: Inserting number of digits before any number

本文关键字:插入 数字 任何 RegExp      更新时间:2023-11-28

我想对包含名称和数字的字符串数组进行排序。但我想改进字母数字排序顺序,以获得类似的顺序

John 8 test
John 9 test
John 10 test

而不是因为"1"<"8"<"9"。我的想法是将一个数字的位数插入到任何数字中,这样在内部要排序的数组就会变成:

John 18 test
John 19 test
John 210 test

它现在是一个字母数字正确排序的数组。

你知道如何以一种简单的方式将数字插入数字后面吗?RegExp将是完美的。我是在nodejs/JavaScript中完成这一切的。

提前感谢!

heinob

我自己找到了(一个)答案:

var a = "John 352 Name 1 test 23 better";
a.replace( /'d+/g, function( match, number) {
    return match.length + match;
});

我想要什么:-)