Get string after last slash javascript
At least three ways: Show
A regular expression:
...which says "grab the series of characters not containing a slash" ( Live example Using lastIndexOf and substring:
Using Array#splitSudhir and Tom Walters have this covered here and here, but just for completeness:
The Get the value of a String after the last Slash #To get the value of a string after the last slash, call the
The parameter we passed to the String.substring method is the start index - the index of the first character to be included in the returned string. We used the String.lastIndexOf method to get the index of the last slash character in the string. We don't want to include the last slash in the returned string, therefore we increment the index by The Note that the An alternative, perhaps simpler approach is to use the
In the code snippet, we first call the
String.split method to split on a This returns an array of strings without the
We then call the Array.pop method which removes and returns the last element of the array. If you care about performance you would use the 1st approach, even though the difference is negligible, unless you're dealing with huge strings. Further Reading #
How do you get the string after the last slash?Use the . substring() method to get the access the string after last slash.
How to get the last string after in JavaScript?Get the value of a String after last Slash in JavaScript. To get the value of a string after the last slash, call the substring() method, passing it the index, after the last index of a / character as a parameter. ... . We don't want to include the last slash in the returned string, therefore we increment the index by 1 .. How do I have JavaScript get a substring after a character?To get the substring after a specific character, call the substring() method, passing it the index after the character's index as a parameter. The substring method will return the part of the string after the specified character. Copied!
How do you grab substring after a specified character jquery or JavaScript?The substr() method extracts a part of a string. The substr() method begins at a specified position, and returns a specified number of characters. The substr() method does not change the original string. To extract characters from the end of the string, use a negative start position.
|