admin管理员组文章数量:1129646
I'm wondering how to remove the first and last character of a string in Javascript.
My url is showing /installers/
and I just want installers
.
Sometimes it will be /installers/services/
and I just need installers/services
.
So I can't just simply strip the slashes /
.
I'm wondering how to remove the first and last character of a string in Javascript.
My url is showing /installers/
and I just want installers
.
Sometimes it will be /installers/services/
and I just need installers/services
.
So I can't just simply strip the slashes /
.
- 1 I'm trying to just strip the first and last character of that. I'm just grabbing the URL – Peanut Commented Nov 25, 2013 at 14:56
- 2 I just wonder why nobody seems to notice that trimming slashes (as the text indicates) and unconditionally removing the first and last char of a string (as the caption indicates) is not quite the same. But it explains the wide range of answers below. – JensG Commented Jan 2, 2017 at 18:05
11 Answers
Reset to default 511Here you go
var yourString = "/installers/";
var result = yourString.substring(1, yourString.length-1);
console.log(result);
Or you can use .slice
as suggested by Ankit Gupta
var yourString = "/installers/services/";
var result = yourString.slice(1,-1);
console.log(result);
Documentation for the slice and substring.
It may be nicer one to use slice
like :
string.slice(1, -1)
I don't think jQuery has anything to do with this. Anyway, try the following :
url = url.replace(/^\/|\/$/g, '');
If you dont always have a starting or trailing slash, you could regex it. While regexes are slower then simple replaces/slices, it has a bit more room for logic:
"/installers/services/".replace(/^\/?|\/?$/g, "")
# /installers/services/ -> installers/services
# /installers/services -> installers/services
# installers/services/ -> installers/services
The regex explained:
- ['start with'
^
] + [Optional?
] + [slash]:^/?
, escaped ->^\/?
- The pipe ( | ) can be read as
or
- ['ends with'
$
] + [Optional?
] + [slash] ->/?$
, escaped ->\/?$
Combined it would be ^/?|/$
without escaping. Optional first slash OR optional last slash.
Technically it isn't "optional", but "zero or one times".
You can do something like that :
"/installers/services/".replace(/^\/+/g,'').replace(/\/+$/g,'')
This regex is a common way to have the same behaviour of the trim
function used in many languages.
A possible implementation of trim function is :
function trim(string, char){
if(!char) char = ' '; //space by default
char = char.replace(/([()[{*+.$^\\|?])/g, '\\$1'); //escape char parameter if needed for regex syntax.
var regex_1 = new RegExp("^" + char + "+", "g");
var regex_2 = new RegExp(char + "+$", "g");
return string.replace(regex_1, '').replace(regex_2, '');
}
Which will delete all /
at the beginning and the end of the string. It handles cases like ///installers/services///
You can also simply do :
"/installers/".substring(1, string.length-1);
You can use substring
method
s = s.substring(0, s.length - 1) //removes last character
another alternative is slice
method
It is too nicer shortcode.
response.data.slice(1,-1) // "Prince"
-> Prince
Use string.slice(1, 0)
to remove the first letter of a string.
Use string.slice(0, -1)
to remove the last letter of a string.
use .replace(/.*\/(\S+)\//img,"$1")
"/installers/services/".replace(/.*\/(\S+)\//img,"$1"); //--> services
"/services/".replace(/.*\/(\S+)\//img,"$1"); //--> services
Others answer are correct for question asked here. If someone searching for case like below. Where multiple /
is added
console.log("///niklesh///raut///".split("/").filter(e=>e).join("/"))
Try:
url=url.substring(1,url.length-1);
This way you can use the directories if it is like .../.../.../...
etc.
本文标签: javascriptHow to remove the first and the last character of a stringStack Overflow
版权声明:本文标题:javascript - How to remove the first and the last character of a string - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736715218a1949161.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论