English 中文(简体)
在档案名称中替换最后数
原标题:Regex to replace the last number in a filename

I m 创建功能,增加1个档案中的最后数目。

样本:

filename01.jpg    =>  filename02.jpg
file01name01.jpg  =>  file01name02.jpg
file01na01me.jpg  =>  file01na02me.jpg

我对原始档案名称中包含相同两倍或两倍以上的案例进行了斗争。 我只想增加档案名称的最后数目。

我做了一些研究,但找不到正确的考试。 也许有人可以帮助我。

Ideally I want my code to look something like this:

var filename = "somefilename";

var newFilename = filename.replace(regex,lastNumber + 1);

我不敢肯定,如果能够这样容易的话。 但是,在我开始发言之前,我需要先说一下。


我最后的解决办法是:

function getIncreasedFileName(fileName){
    var newFileName = fileName.replace(/(D+)(d+)(D+)$/, 
        function(str, m1, m2, m3) {
            var newstr = (+m2 + 1) + "";

            return m1 + new Array(m2.length+1 - newstr.length).join("0") + newstr + m3;
        })

    return newFileName;
}
最佳回答
"test01.jpg".replace(/(D+)(d+)(D+)$/, function(str, m1, m2, m3) {
    var newstr = (+m2 + 1) + "";
    return m1 + new Array(3 - newstr.length).join("0") + newstr + m3;
    // m1 === "test"
    //
    // m2 === "01"
    //   So:
    //      +m2 + 1 === 2
    //
    //      (+m2 + 1) + "" === "2"
    //         (convert to string, call this `newstr`)
    //
    //      new Array(3 - newstr.length).join("0") === "0"
    //         (this adds an appropriate amount of leading zeroes
    //         so that the total length is 2)
    //
    // m3 === ".jpg"
});

Where:

D+   one or more non-digits
d+   one or more digits
D+   one or more non-digits
$     end of string (so it only matches the last digits)

接着,在做以下工作后,用一种恢复新护法的职能取代相应的扼杀:

  1. adding 1 to the number
  2. adding leading zeroes
问题回答

Here s what I came up with:

var newFilename = filename.replace(/(d+)(D*$)/g ,
                      function(match, c1, c2) {
                        var nextNum = (+c1 + 1);
                        return (nextNum < 10 ? "0" + nextNum : nextNum) + c2;
                      });

注 从最后数字到插图结束,将“档案名称.01”改为“档案名称.02”和“名称01”改为“名称02”。 如果你只想在编号后有其他特性的地方对编号作一改动,则*改为+,以对应one,或最终将更多的非数字。

各位首先可以找到最后一位数,增加数,然后取而代之:

m = filename.match(/([0-9][0-9])([^0-9]*)$/);
incr = parseInt(m[1]) +1;
newFilename = filename.replace(/([0-9][0-9])([^0-9]*)$/, incr + "$2");

这并没有增加一例婴儿的出生人数低于零。 我要向你表示。





相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签