简体   繁体   中英

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.

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 / .

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

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

Documentation for the slice and substring .

使用slice可能会更好:

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

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")

"/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);

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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