views:

49

answers:

3

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

How do I do that?

The prefix before _ can change.

+1  A: 

Assuming your string always starts with 'test_':

var str = 'test_23';
alert(str.substring('test_'.length));
BoltClock
what if it doesn't start with test?
DerNalia
See Andy E's answer.
BoltClock
+3  A: 

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()
pop()

Andy E
+1 I like this.
Marc
A: 
string = "test_1234";
alert(string.substring(string.indexOf('_')+1));

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

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