jQuery如何在最后删除或替换文本

时间:2013-08-30 04:09:00

标签: jquery

我可以在最后使用点(。)

后删除或替换任何文本
ex)OUTsoundfile.123054236.123054236.wav 

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

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

5 个答案:

答案 0 :(得分:4)

尝试

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

FIDDLE

答案 1 :(得分:1)

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

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

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

答案 2 :(得分:1)

使用替换为regexp,例如

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

详细了解replace function

答案 3 :(得分: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(".");

答案 4 :(得分:0)

尝试

var trimwav = "ex)OUTsoundfile.123054236.123054236.wav";
alert(trimwav.substr(0,x.lastIndexOf(".")));