144

Let’s say I have test_23 and I want to remove test_.

How do I do that?

The prefix before _ can change.

1

7 Answers 7

196

My favourite way of doing this is "splitting and popping":

var str = "test_23";
alert(str.split("_").pop());
// -> 23

var str2 = "adifferenttest_153";
alert(str2.split("_").pop());
// -> 153

split() splits a string into an array of strings using a specified separator string.
pop() removes the last element from an array and returns that element.

Sign up to request clarification or add additional context in comments.

6 Comments

The C programmer in me cringes at the number of objects created and destroyed on this simple split/pop operation :) Probably: an array, 2 string, another array, another string.
@xgbi: yeah, JavaScript and C developers have rather different philosophies, though ;-)
You could add to your answer the possibility of using alert(str.split("_")[1]); since .split creates an array with two elements, [0] being before the "_" and [1] after. This was actually what I was looking for, but your answer helped me get there^^
@FernandoSilva Then, say if there were two "_" or more, we'd get 3 item array and be able to use/delete the third one too, right? BTW, +1. Exactly what I was looking for.
@TheLightSabrix take a look at it working, it's much easier than trying to explain it in text. jsbin.com/keleguzacu/edit?js,console
|
93

If you want to remove part of string

let str = "try_me";
str.replace("try_", "");
// me

If you want to replace part of string

let str = "try_me";
str.replace("try_", "test_");
// test_me

1 Comment

If you want to replace all matches, use str.replaceAll("try_", "");
52

Assuming your string always starts with 'test_':

var str = 'test_23';
alert(str.substring('test_'.length));

2 Comments

what if it doesn't start with test?
use replace but that doesn't work if there are multiple occurences. str.replace("test_", ""); will return a new string where all occurences of "test_" are removed, so if only one occurence is guaranteed, then it should be fine. However, if there are multiple, I would use str = str.substring(0, str.indexOf("test_)" + str.substring(str.indexOf("test_") + 5, str.length); I know this is an old post but, if anyone comes across it, might help at least one person out there somewhere.
23

Easiest way I think is:

var s = yourString.replace(/.*_/g,"_");

Comments

12
string = "test_1234";
alert(string.substring(string.indexOf('_')+1));

It even works if the string has no underscore. Try it at http://jsbin.com/

1 Comment

This has less overhead, but more code than Andy E's answer. Both work, but I prefer this method.
1

let text = 'test_23';
console.log(text.substring(text.indexOf('_') + 1));

Comments

-1

You can use the slice() string method to remove the begining and end of a string

const str = 'outMeNo';

const withoutFirstAndLast = str.slice(3, -2);
console.log(withoutFirstAndLast);// output-->  'Me'

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.