How to substring in jquery
Advertisement
How to substring in jquery
Question
How can I use jquery on the client side to substring "nameGorge" and remove "name" so it outputs just "Gorge"?
var name = "nameGorge"; //output Gorge
2010/11/08
Accepted Answer
No jQuery needed! Just use the substring method:
var gorge = name.substring(4);
Or if the text you want to remove isn't static:
var name = 'nameGorge';
var toRemove = 'name';
var gorge = name.replace(toRemove,'');
2010/11/08
Read more... Read less...
Using .split()
. (Second version uses .slice()
and .join()
on the Array.)
var result = name.split('name')[1];
var result = name.split('name').slice( 1 ).join(''); // May be a little safer
Using .replace()
.
var result = name.replace('name','');
Using .slice()
on a String.
var result = name.slice( 4 );
2010/11/08
Standard javascript will do that using the following syntax:
string.substring(from, to)
var name = "nameGorge";
var output = name.substring(4);
Read more here: http://www.w3schools.com/jsref/jsref_substring.asp
2010/11/08
You don't need jquery in order to do that.
var placeHolder="name";
var res=name.substr(name.indexOf(placeHolder) + placeHolder.length);
2010/11/08
Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Email: [email protected]