Remove everything after last backslash
You need lastIndexOf
and substr
...
var t = "\\some\\route\\here";
t = t.substr(0, t.lastIndexOf("\\"));
alert(t);
Also, you need to double up \
chars in strings as they are used for escaping special characters.
Update
Since this is regularly proving useful for others, here's a snippet example...
// the original stringvar t = "\\some\\route\\here";
// remove everything after the last backslashvar afterWith = t.substr(0, t.lastIndexOf("\\") + 1);
// remove everything after & including the last backslashvar afterWithout = t.substr(0, t.lastIndexOf("\\"));
// show the resultsconsole.log("before : " + t);console.log("after (with \\) : " + afterWith);console.log("after (without \\) : " + afterWithout);
Regular Expression, remove everything after last forward slash
Replace /[^/]*$
with an empty string
Remove everything after last forward slash in Google Sheets with Regex
You may use
=REGEXREPLACE(A1, "(/[^/]+)(?:/\?[^/]*)?$", "$1/")
Details
(/[^/]+)
- Capturing group 1:/
and then 1+ chars other than/
(?:/\?[^/]*)?
- an optional non-capturing group matching/
- a slash\?
- a?
char[^/]*
- 0 or more chars other than/
$
- end of string.
The replacement is $1
, the contents of Group 1 and a /
that must be present (adding it where it is missing).
How to Remove Everything after the last Slash in a Path Powershell
You can do the following:
(Get-ADUser Identity USER -Properties CanonicalName).CanonicalName -replace '/[^/]*$'
/
matches literal /
. [^]
is a negated character class making [^/]
match anything but /
. *
is zero or more matches quantifier (greedily matching). $
is the end of string.
Path conversions do work as well, but if your platform's directory separator char is not /
, then the /
will be converted to something else. Then you will be left with converting them back. It is just more work than using the simple -replace
operator.
Replace everything after last forward slash
You need to assign the return value of replaceFirst
method to target
as no method in String
class changes the String object itself:
target = target.replaceFirst("[^/]*$", replace);
JS remove everything after the last occurrence of a character
Here you are:
var URL = "http://stackoverflow.com/questions/10767815/remove-everything-before-the-last-occurrence-of-a-character";alert(URL.substring(0, URL.lastIndexOf("/") + 1));
Remove everything after backslash in base R?
Here is another way you could try:
gsub("(^\\d+)([\a-zA-Z0-9]*)", "\\1", myString)
[1] "5" "10" "100"
Related Topics
Regular Expression to Accept Both Positive and Negative Numbers
How to Assign [(Ngmodel)] With an Empty/Null/Undefined Object - Angular 4
How to Change the Image(Slider) One by One Every 3 Seconds in JavaScript
Swiper Slider - Thumbs Gallery With Slidetoclickedslide Do Not Work Properly
How to Mock a Fake Database for When Unit Testing Against Knex
Text Box to Appear When a Radio Button Is Selected
Number Value Format With Comma and Two Decimal Points
How to Fetch Url of Current Tab in My Chrome Extension Using JavaScript
Adding Whitespace to a String in Typescript (In Angular)
How to Instantiate a File Object in JavaScript
Jest Encountered an Unexpected Token: Syntaxerror: Unexpected Token {
Typescript Property Does Not Exist on Type {}
Vuejs Error, Invalid Prop: Type Check Failed for Prop. Expected Date, Got Number With Value
How to Set Top and Bottom Margin in Addhtml
Have a Div Move on Scroll Up and Down