[英]How to trim a string after a specific character in javascript
I'm having a string like "image.jpg". 我有一个类似“ image.jpg”的字符串。 I want to save the image with its name, so i need to trim the image extension.
我想用它的名称保存图像,所以我需要修剪图像扩展名。 Any methods in javascript to achieve that.
javascript中的任何方法都可以实现。
str = 'image.test.jpg'
str.slice(0,str.lastIndexOf('.'))
This accounts for having .
这占了
.
within the name of the image 在图像名称内
You need to take .
您需要采取
.
in the base name into account. 在基本名称中考虑。 This remove the last
.ext
value 这将删除最后一个
.ext
值
var fileName = "some.image.jpg"; var baseName = fileName.replace(/\\.[^.]+$/, ''); $('div').text(baseName);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> <div></div>
There is a split function offered by javascript. javascript提供了拆分功能 。 Essentially what you are trying to do is split the string into array of strings based on the character you need.
本质上,您要尝试的是根据所需字符将字符串拆分为字符串数组。
var str = "image.jpg";
var array = str.split(".");
array.pop();
var imageName = array.join("");
PS: This is pure javascript so you don't really need any other library. PS:这是纯JavaScript,因此您实际上不需要任何其他库。
You can use javascript: 您可以使用javascript:
yourString = "image.jpg";
yourString=yourString.replace('.jpg','');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.