Advertisement
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
1
115
11/8/2010 6:25:06 PM

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
235
11/8/2010 6:28:43 PM

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

That's just plain JavaScript: see substring and substr.

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

var name = "nameGorge";
name.match(/[A-Z].*/)[0]
2010/11/08

Source: https://stackoverflow.com/questions/4126762
Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Email: [email protected]