How to extract a part of a string in javascript? [duplicate] - javascript

This question already has answers here:
Last segment of URL with JavaScript
(30 answers)
Closed 1 year ago.
I have a string which is
file:///storage/emulated/0/Android/data/com.myapp/files/Pictures/00cb3b0b-45d5-486b-8f33-0fa3bf2285ff.jpg
And I want to extract the string after the last / which will be 00cb3b0b-45d5-486b-8f33-0fa3bf2285ff.jpg
How can I extract the last part of this string and store it in a variable ?

I extracted the last part of string like this :
mystring.substring(mystring.lastIndexOf('/') + 1)

Related

How to get remaining values except matches in JavaScript Regex Match [duplicate]

This question already has answers here:
Split string into array without deleting delimiter?
(5 answers)
JS string.split() without removing the delimiters [duplicate]
(10 answers)
Closed 2 years ago.
String ${provider.name} - ${item.publication_time|date|relative}
Regex /\${([^}]+)}/gmi
From string.match(/\${([^}]+)}/gmi) this, I am getting ["${provider.name}", "${item.publication_time|date|relative}"] as output.
But I need other unmatched parts of the string in the output array. My expected result should be ["${provider.name}", " - ", "${item.publication_time|date|relative}"]
Can you please suggest me how can I achieve this by changing the regex?

How to replace a string in javascript only when it's comes before another string using regex? [duplicate]

This question already has answers here:
REGEX: Capture Filename from URL without file extension
(5 answers)
JavaScript Regexp to replace a value only when it follows certain characters?
(2 answers)
Closed 3 years ago.
I want to replace xx with . in this string 100x100/<name>xx-somename-xxjpg but only xx comes next to jpg.
'100x100/<name>xx-somename-xxjpg'.replace(/(xx(jpg|jpeg|png))/,'$')
What I get -> 100x100/<name>xx-somename-$
What I expect-> 100x100/<name>xx-somename-.jpg
How do I do it?
'100x100/<name>xx-somename-xxjpg'.replace(/(xx)(?=jpg|jpeg|png)/,'.')

How to format string in JS? [duplicate]

This question already has answers here:
JavaScript .replace only replaces first Match [duplicate]
(7 answers)
Closed 6 years ago.
I would like to have the result :
28,12,2016
From this string "28/12/2016"
I tried :
("28/12/2016").replace('/',',');
==>"28,12/2016"
I don't know how to delete the second /and the " "
use split and join method
var a="28/12/2016";
var ans=a.split("/").join(",");
console.log(ans);

How do I get the last character with jquery [duplicate]

This question already has answers here:
How to get the last character of a string?
(15 answers)
Closed 8 years ago.
I have a string I am trying to get the last character with jquery. How would I do that?
var stringTemp = "fieldNameWithIndex_1";
stringTemp.charAt(stringTemp.length-1);
or
stringTemp.slice(-1);
Use .split():
stringTemp.split('_')[1]

how to remove the following text from a string in javascript? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Replace all spaces in a string with '+'
how to remove ,#contacts.Assessor.firstname# #contacts.Assessor.lastname# and
,#contacts.Assessor - Secondary.firstname# #contacts.Assessor - Secondary.lastname# from a
string in javascript?
Thanks
Simply use .replace()
string = string
.replace(',#contacts.Assessor - Secondary.firstname# #contacts.Assessor - Secondary.lastname#', '')
.replace(',#contacts.Assessor.firstname# #contacts.Assessor.lastname#', '');
Also take a loot at this related question

Categories

Resources