119

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
Gorge
  • 1,399
  • 3
  • 9
  • 11
  • 15
    jQuery is a library written in JavaScript. It's used on top of JavaScript. What you are asking can be done in JavaScript without the need to use the jQuery library at all. – Jeff Nov 08 '10 at 18:35
  • 1
    jQuery IS JavaScript (a JavaScript library to be exact, but JavaScript, nonetheless). That having been said, I don't think using jQuery to get a substring could ever be written any easier than it already is in straight JavaScript. – VoidKing Jun 18 '13 at 19:46
  • Hi. I am the future. I have built a plugin for jQuery to do substrings for vanilla JS. Please use it like `let result = $.substring(value, idx)` - We have much fun in the future! – Piotr Kula Oct 10 '22 at 21:16

8 Answers8

238

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,'');
Justin Niessner
  • 242,243
  • 40
  • 408
  • 536
  • 5
    Sorry, didn't expect this to blow up so hard after I went to sleep. I've cleaned things up here a little. Meanwhile, somebody needs to read [this](http://meta.stackexchange.com/questions/19478/the-many-memes-of-meta/19492#19492) – BoltClock Jan 24 '13 at 07:21
  • Hey BoltClock: How are you doing? I was trying to prove a point in here, thanks very much.. so that means that if I am trying to revamp a code for a client, and I come here looking for an answer, and the op's marked answer is satisfactory for him.. then I cannot simply post the same question nor to give an example of my problem.. very interesting.. so my question was like how are these guys so certain that if I am running a jQuery function and inside I use .split I am refering to the native JS way or some sort of tunneling or proxy to trigger that through jQuery. Also if you read the question. –  Jan 25 '13 at 22:49
  • My point is that to me is working within the jQuery namespace, I really don't care much if the case is that is calling directly string.split from JS, but instead from the community I have understood that if I post that exact question, it will be marked as narrowed, as this response is more accepted than the fact that there is a jQuery.split documentation reference somewhere to me at least tells me that it should be because it's there without giving any conflict to my code and all the jQuery plugins that on the deleted pictures you were able to see... so thanks once more –  Jan 25 '13 at 22:51
  • 5
    @JeanPaul - jQuery has no split(). We've already been over this. Both articles you linked to previously used text() to get a vanilla JavaScript string and then called string.split(). You're just plain wrong in this case. Drop it. – Justin Niessner Jan 26 '13 at 00:27
53

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 );
user113716
  • 318,772
  • 63
  • 451
  • 440
18

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

drew
  • 1,312
  • 8
  • 20
9

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

casablanca
  • 69,683
  • 7
  • 133
  • 150
  • 1
    Or indeed [`slice()`](http://docs.sun.com/source/816-6408-10/string.htm#1194366). Strange JS should have so many ways to say the same thing, all subtly different. `slice()` is my preferred method as it behaves consistently with `slice()` on arrays, and allows negative start and end indexes for index-from-end. – bobince Nov 08 '10 at 19:08
  • @bobince: True, and I never remember which of `substring` and `substr` takes a length parameter and which takes an end index - I always need to look up the docs to find out. – casablanca Nov 08 '10 at 20:05
5

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

var placeHolder="name";
var res=name.substr(name.indexOf(placeHolder) + placeHolder.length);
  • I need to do it on the client side and I already have jquery. What are you suggesting then? Sorry for the simple question, but I'm fairly a beginner with js and jquery. – Gorge Nov 08 '10 at 18:30
  • That statement would return "nameGorge" because indexOf("name") would return 0. – Justin Niessner Nov 08 '10 at 18:30
2
var name = "nameGorge";
name.match(/[A-Z].*/)[0]
0

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 with 'name' will achieve the desired result. What's the criteria for getting your substring, exactly?

aaronofleonard
  • 2,546
  • 17
  • 23
-1

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>
Jean G.T
  • 1
  • 10
  • 25