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

Share Improve this question edited Aug 25, 2016 at 17:30 Emile Bergeron 17.4k5 gold badges84 silver badges131 bronze badges asked Nov 25, 2013 at 14:54 PeanutPeanut 3,2857 gold badges29 silver badges47 bronze badges 2
  • 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
Add a comment  | 

11 Answers 11

Reset to default 511

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.

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