开发者

Can I use search/replace and regular expression to increment a counter?

I have a list of file names which I want to change to include a counter:

asdf.jpg to become  id:001, name:asdf.jpg
lkh.jpg  to become  id:002, name:lkh.jpg

In Aptana Studio 3 editor can I use a search/replace regular expression to increment the counter?

T开发者_运维百科hanks


There is no way to do an indexed replace like you described using regular expressions. If your list is sequential, and not wrapped up in markup, then a quick script in any language could handle the replacements for you. For instance, in JavaScript...

(This code is quick and dirty, has no friends, and was teased in grade school, but gets the job done.)

<html>
<script>
function init(){
    var i,id=1,item,arr=[],items=document.body.innerHTML.split(/[\r\n]/g);
    for(i=0;i<items.length;i++){
        item=items[i].replace(/^\s+|\s+$/g,"");
        if(item=='')
            continue;
        arr[arr.length] = 'id:'+
            ('00'.substr(0,2-Math.floor(Math.log(id)/Math.LN10))+id)+
            ', name:'+item;
        id++;
    }
    document.write(arr.join('<br>'));
}
</script>
<body onload='init()'>
asdf.jpg
lkh.jpg
gfh.jpg
iuaa.jpg
(etc...)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜