最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How can I loop through string and replace all periods except the last one? - Stack Overflow

programmeradmin1浏览0评论

Let's say I have a string like this:

var test = my.long.file.name.zip

I am getting the total number of periods in this string with javascript like so:

var dots = (test.match(/\./g) || []).length;

I would then like to replace all of the periods in the string with underscores if there is more than one period in the string.

   if(dots>"1"){   
     var newname = test.replace(/\./g, "_");
     console.log(newname);
    }

The problem is that this is replacing all of the periods. I would like to keep the last on intact. So what I would like the newname variable to read as would be:

my_long_file_name.zip

My guess is that I should use $.each() somehow to iterate over all except the last one to change the name. How should I do this?

Let's say I have a string like this:

var test = my.long.file.name.zip

I am getting the total number of periods in this string with javascript like so:

var dots = (test.match(/\./g) || []).length;

I would then like to replace all of the periods in the string with underscores if there is more than one period in the string.

   if(dots>"1"){   
     var newname = test.replace(/\./g, "_");
     console.log(newname);
    }

The problem is that this is replacing all of the periods. I would like to keep the last on intact. So what I would like the newname variable to read as would be:

my_long_file_name.zip

My guess is that I should use $.each() somehow to iterate over all except the last one to change the name. How should I do this?

Share Improve this question edited Feb 13, 2015 at 16:44 Jamiec 136k15 gold badges141 silver badges199 bronze badges asked Feb 13, 2015 at 16:42 codacopiacodacopia 2,5119 gold badges41 silver badges59 bronze badges 2
  • There is nothing jQuery about this - its vanilla javascript so I updated your question. – Jamiec Commented Feb 13, 2015 at 16:44
  • Ok, thanks. I am just doing this in a jQuery function so I was focused on that. – codacopia Commented Feb 13, 2015 at 16:45
Add a ment  | 

4 Answers 4

Reset to default 6

You dont necessarily need a loop, you could do it with a more plex regex, which uses a positive lookahead

The regex /\.(?=.*\.)/g finds periods, but only where there is a subsequent period somewhere further along, which means the last one is not matched.

window.onload = function(){
  var input = "my.long.file.name.zip"
  var result = input.replace(/\.(?=.*\.)/g,'_')

  alert(result);
}

Consider splitting the string on '.', then re-joining all but the last with '_':

var test = "my.long.file.name.zip";

parts = test.split('.');

var plen = parts.length;

if (plen > 1) {
  test = parts.slice(0, plen - 1).join('_') +
    "." +
    parts[plen - 1];
}

console.log(test);

a lookahead group in regex will work:

var test = 'my.long.file.name.zip';
var result = test.replace(/\.(?=[^.]*\.)/g, '_');
alert(result);

this matches a dot followed by ('anything but dot' and another dot), replacing only what is outside the group

var test = 'my.long.file.name.zip';
var last_index = test.lastIndexOf('.');
var newname = test;

if (-1 !== last_index) {
  newname = test.replace(/\./g, '_');
  newname = newname.substring(0, last_index).concat('.', newname.substring(last_index + 1));
}

console.log(newname);
发布评论

评论列表(0)

  1. 暂无评论