简体   繁体   English

如何删除字符串的第一个和最后一个字符

[英]How to remove the first and the last character of a string

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 显示/installers/我只想要installers

Sometimes it will be /installers/services/ and I just need installers/services .有时它会是/installers/services/而我只需要installers/services

So I can't just simply strip the slashes / .所以我不能简单地去掉斜线/

Here 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或者您可以按照Ankit Gupta的建议使用.slice

 var yourString = "/installers/services/"; var result = yourString.slice(1,-1); console.log(result);

Documentation for the slice and substring . slicesubstring的文档。

使用slice可能会更好:

string.slice(1, -1)

I don't think jQuery has anything to do with this.我认为 jQuery 与此无关。 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管道 ( | ) 可以读作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.此正则表达式是具有与许多语言中使用的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///它处理像///installers/services///这样的情况

You can also simply do :你也可以简单地做:

"/installers/".substring(1, string.length-1);

You can use substring method您可以使用substring方法

s = s.substring(0, s.length - 1) //removes last character

another alternative is slice method另一种选择是slice方法

if you need to remove the first leter of string如果您需要删除字符串的第一个字母

string.slice(1, 0)

and for remove last letter删除最后一个字母

string.slice(0, -1)

use .replace(/.*\/(\S+)\//img,"$1")使用.replace(/.*\/(\S+)\//img,"$1")

"/installers/services/".replace(/.*\/(\S+)\//img,"$1"); //--> services

"/services/".replace(/.*\/(\S+)\//img,"$1"); //--> services

It is too nicer shortcode.它是更好的简码。

response.data.slice(1,-1) // "Prince"

-> Prince

以下正则表达式将删除第一个和最后一个斜杠(如果存在)

var result = "/installers/services/".match(/[^/].*[^/]/g)[0];
url=url.substring(1,url.Length-1);

这样,您可以使用目录,如果它像 .../.../.../... 等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM