1

最后出现点(。)后,我可以删除或替换任何文本吗?

ex)OUTsoundfile.123054236.123054236.wav 

我想删除 .wav 或用空字符串替换 .wav

无法使用subString,因为输入文本中可能不存在 .wav。

4

5 回答 5

4

尝试

x.substring(0, x.lastIndexOf("."));

FIDDLE

于 2013-08-30T04:18:04.143 回答
1

这将是正确的正则表达式:

var myString = "ex)OUTsoundfile.123054236.123054236.wav";
var output = myString.replace(/\.[^.]*$/, '');

http://jsfiddle.net/samliew/3UdLH/

于 2013-08-30T04:11:52.630 回答
1

使用正则表达式替换,例如

 'OUTsoundfile.123054236.123054236.wav'.replace(/\.wav/,'');

阅读有关替换功能的更多信息

于 2013-08-30T04:12:01.500 回答
0

尝试

var trimwav = "ex)OUTsoundfile.123054236.123054236.wav";
alert(trimwav.substr(0,x.lastIndexOf(".")));
于 2013-08-30T04:34:12.593 回答
0

使用普通的旧 JavaScript:

var filename = "OUTsoundfile.123054236.123054236.wav";
var pieces = filename.split(".");
  // pieces is an array that looks like this:
  // ["OUTsoundfile", "123054236", "123054236", "wav"]

  // Remove the last element from pieces, i.e. "wav"
  // If you want to do anything with this last piece, such as check what the
  // piece you removed was, use the return value of this statement.
pieces.pop();
  // pieces now just looks like this:
  // ["OUTsoundfile", "123054236", "123054236"]

  // Put the string back together
var newFilename = pieces.join(".");
  // newFilename is now this string: "OUTsoundfile.123054236.123054236"

或者,没有评论:

var filename = "OUTsoundfile.123054236.123054236.wav";
var pieces = filename.split(".");
pieces.pop();
var newFilename = pieces.join(".");
于 2013-08-30T04:12:25.623 回答