How to remove part of a string?

Solution 1:

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.

Solution 2:

Assuming your string always starts with 'test_':

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

Solution 3:

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

Solution 4:

Easiest way I think is:

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