I'm wondering how to remove the first and last character of a string in Javascript.
我想知道如何删除Javascript中字符串的第一个和最后一个字符。
My url is showing /installers/
and I just want installers
.
我的url显示/安装程序/我只想安装程序。
Sometimes it will be /installers/services/
and I just need installers/services
.
有时是/安装程序/服务/我只需要安装程序/服务。
So I can't just simply strip the slashes /
.
所以我不能简单地去掉斜线/。
9 个解决方案
#1
#2
92
It may be nicer one to use slice like :
使用切片可能更好:
string.slice(1, -1)
#3
9
I don't think jQuery has anything to do with this. Anyway, try the following :
我认为jQuery与此无关。无论如何,试试下面的方法:
url = url.replace(/^\/|\/$/g, '');
#4
5
You could regex it:
你可以正则表达式:
"string".replace(/^\/?|\/?$/, "")
"/installers/services/".replace(/^\/?|\/?$/, "") // -> installers/services
The regex explained:
- Optional first slash: ^/?
, escaped -> ^\/?
(the ^
means beginning of string)
- The pipe ( | ) can be read as or
- Than the option slash at the end -> /?$
, escaped -> \/?$
( the $
means end of string)
正则表达式解释说:-可选的第一个削减:^ / ?,逃- > ^ \ / ?(^表示字符串的开始)-管道(|)可以读或比最后选择削减————> / ?美元,逃- > \ / ?$($表示字符串的结束)
Combined it would be ^/?|/$
without escaping. Optional first slash OR optional last slash
结合将是^ / ?| / $没有逃离。可选的第一个斜杠或可选的最后一个斜杠
#5
2
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.
这个regex是一种具有许多语言中使用的trim函数的相同行为的常见方法。
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);
#6
0
You can use substring
method
可以使用子字符串方法
s = s.substring(0, s.length - 1) //removes last character
another alternative is slice
method
另一种选择是slice方法
#7
0
use .replace(/.*\/(\S+)\//img,"$1")
使用.replace(/ . * \ / \ / / img(\ S +),“$ 1”)
"/installers/services/".replace(/.*\/(\S+)\//img,"$1"); //--> services
"/services/".replace(/.*\/(\S+)\//img,"$1"); //--> services
#8
-1
The following regex will strip the first and last slashes if they exist,
下面的regex将删除第一个和最后一个斜杠,如果它们存在,
var result = "/installers/services/".match(/[^/].*[^/]/g)[0];
#9
-3
url=url.substring(1,url.Length-1);
This way you can use the directories if it is like .../.../.../... etc.
这样,如果目录是…/…/…等。
#1
225
Here you go
在这里你去
var yourString = "/installers/";
var result = yourString.substring(1, yourString.length-1);
Or you can use .slice
as suggested by Anka Gupta
或者你也可以像Anka Gupta建议的那样使用。slice
var result = yourString.slice(1, -1);
Documentation for the slice and substring.
切片和子字符串的文档。
#2
92
It may be nicer one to use slice like :
使用切片可能更好:
string.slice(1, -1)
#3
9
I don't think jQuery has anything to do with this. Anyway, try the following :
我认为jQuery与此无关。无论如何,试试下面的方法:
url = url.replace(/^\/|\/$/g, '');
#4
5
You could regex it:
你可以正则表达式:
"string".replace(/^\/?|\/?$/, "")
"/installers/services/".replace(/^\/?|\/?$/, "") // -> installers/services
The regex explained:
- Optional first slash: ^/?
, escaped -> ^\/?
(the ^
means beginning of string)
- The pipe ( | ) can be read as or
- Than the option slash at the end -> /?$
, escaped -> \/?$
( the $
means end of string)
正则表达式解释说:-可选的第一个削减:^ / ?,逃- > ^ \ / ?(^表示字符串的开始)-管道(|)可以读或比最后选择削减————> / ?美元,逃- > \ / ?$($表示字符串的结束)
Combined it would be ^/?|/$
without escaping. Optional first slash OR optional last slash
结合将是^ / ?| / $没有逃离。可选的第一个斜杠或可选的最后一个斜杠
#5
2
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.
这个regex是一种具有许多语言中使用的trim函数的相同行为的常见方法。
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);
#6
0
You can use substring
method
可以使用子字符串方法
s = s.substring(0, s.length - 1) //removes last character
another alternative is slice
method
另一种选择是slice方法
#7
0
use .replace(/.*\/(\S+)\//img,"$1")
使用.replace(/ . * \ / \ / / img(\ S +),“$ 1”)
"/installers/services/".replace(/.*\/(\S+)\//img,"$1"); //--> services
"/services/".replace(/.*\/(\S+)\//img,"$1"); //--> services
#8
-1
The following regex will strip the first and last slashes if they exist,
下面的regex将删除第一个和最后一个斜杠,如果它们存在,
var result = "/installers/services/".match(/[^/].*[^/]/g)[0];
#9
-3
url=url.substring(1,url.Length-1);
This way you can use the directories if it is like .../.../.../... etc.
这样,如果目录是…/…/…等。