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

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]

Related

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

Replacing “index” array string using Javascript regex [duplicate]

This question already has answers here:
Regex using javascript to return just numbers
(14 answers)
Closed 6 years ago.
with the following code can get the 1 in the string.
var match = /[0-9]+/.exec('[1][2]');
console.log(match);
How do i get the 2 and not the 1 ?
Try with this regex
console.log(/(\[[0-9]\])+/.exec('[1][2]'));
I think it's only a matter of escaping the square brackets

extracting text between two characters [duplicate]

This question already has answers here:
Regular expression to get a string between two strings in Javascript
(13 answers)
Closed 6 years ago.
Having a string like:
"*this* not that"
I can select *this*
\*(.*?)\*
but I'm not able to get only this.
what I am trying to achieve is to replace -this- by a new string. What's the easiest way to do that ?
you can try:
"*this* not that".replace(/\*.*\*/,'*new_string*');
//"*new_string* not that"

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

how to split into character group? [duplicate]

This question already has answers here:
Split large string in n-size chunks in JavaScript
(23 answers)
Closed 9 years ago.
I want to split string
"abcdefgh"
to
"ab","cd","ef","gh"
using javascript split()
"abcdefgh".split(???)
Can you please help ?
Instead of split, try match:
var text = "abcdefgh";
print(text.match(/../g));
prints:
ab,cd,ef,gh
as you can see on Ideone.

Categories

Resources