NullVoxPopuli NullVoxPopuli - 2 months ago 7
Javascript Question

How to remove part of a string?

Let’s say I have

test_23
and I want to remove
test_
.

How do I do that?

The prefix before
_
can change.

Answer

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