How to substring in jquery

JavascriptJqueryString

Javascript Problem Overview


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

Javascript Solutions


Solution 1 - Javascript

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

Solution 2 - Javascript

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

Solution 3 - Javascript

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

Solution 4 - Javascript

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

Solution 5 - Javascript

You don't need jquery in order to do that.

var placeHolder="name";
var res=name.substr(name.indexOf(placeHolder) + placeHolder.length);

Solution 6 - Javascript

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

Solution 7 - Javascript

Yes you can, although it relies on Javascript's inherent functionality and not the jQuery library.

http://www.w3schools.com/jsref/jsref_substr.asp The substr function will allow you to extract certain parts of the string.

Now, if you're looking for a specific string or character to use to find what part of the string to extract, you can make use of the indexOf function as well. http://www.w3schools.com/jsref/jsref_IndexOf.asp

The question is somewhat vague though; even just [link text][1] with 'name' will achieve the desired result. What's the criteria for getting your substring, exactly?

[1]: http://www.w3schools.com/jsref/jsref_replace.asp "replace"

Solution 8 - Javascript

How about the following?

<script charset='utf-8' type='text/javascript'>
  jQuery(function($) { var a=$; a.noConflict();
    //assumming that you are using an input text 
    //  element with the text "nameGorge"
    var itext_target = a("input[type='text']:contains('nameGorge')");
    //gives the second part of the split which is 'Gorge'
    itext_target.html().split("nameGorge")[1];
    ...
  });
</script>

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionGorgeView Question on Stackoverflow
Solution 1 - JavascriptJustin NiessnerView Answer on Stackoverflow
Solution 2 - Javascriptuser113716View Answer on Stackoverflow
Solution 3 - JavascriptdrewView Answer on Stackoverflow
Solution 4 - JavascriptcasablancaView Answer on Stackoverflow
Solution 5 - JavascriptMaurizio CucchiaraView Answer on Stackoverflow
Solution 6 - Javascriptuser372551View Answer on Stackoverflow
Solution 7 - JavascriptaaronofleonardView Answer on Stackoverflow
Solution 8 - JavascriptJean G.TView Answer on Stackoverflow