[javascript] 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.

This question is related to javascript string

The answer is


If you want to remove part of string

let str = "test_23";
str.replace("test_", "");
// 23

If you want to replace part of string

let str = "test_23";
str.replace("test_", "student-");
// student-23

Assuming your string always starts with 'test_':

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

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

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


Dynamically, if you want to remove (a) part(s) from (a) fixed index(es) of a string, use this function:

/**
 * Removes index/indexes from a string, using a delimiter.
 * 
 * @param string $string
 * @param int|int[] $index An index, or a list of indexes to be removed from string.
 * @param string $delimiter 
 * @return string
 * @todo Note: For PHP versions lower than 7.0, remove scalar type hints (i.e. the
 * types before each argument) and the return type.
 */
function removeFromString(string $string, $index, string $delimiter = " "): string
{
    $stringParts = explode($delimiter, $string);

    // Remove indexes from string parts
    if (is_array($index)) {
        foreach ($index as $i) {
            unset($stringParts[(int)($i)]);
        }
    } else {
        unset($stringParts[(int)($index)]);
    }

    // Join all parts together and return it
    return implode($delimiter, $stringParts);
}

For your purpose:

remove_from_str("REGISTER 11223344 here", 1); // Output: REGISTER here

One of its usages is to execute command-like strings, which you know their structures.


The following snippet will print "REGISTER here"

$string = "REGISTER 11223344 here";

$result = preg_replace(
   array('/(\d+)/'),
   array(''),
   $string
);

print_r($result);

The preg_replace() API usage us as given below.

$result = preg_replace(
    array('/pattern1/', '/pattern2/'),
    array('replace1', 'replace2'),
    $input_string
);

Do the following

$string = 'REGISTER 11223344 here';

$content = preg_replace('/REGISTER(.*)here/','',$string);

This would return "REGISTERhere"

or

$string = 'REGISTER 11223344 here';

$content = preg_replace('/REGISTER (.*) here/','',$string);

This would return "REGISTER here"


Easiest way I think is:

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

string = "removeTHISplease";
result = string.replace('THIS','');

I think replace do the same thing like a some own function. For me this works.


str_replace(find, replace, string, count)
  • find Required. Specifies the value to find
  • replace Required. Specifies the value to replace the value in find
  • string Required. Specifies the string to be searched
  • count Optional. A variable that counts the number of replacements

As per OP example:

$Example_string = "REGISTER 11223344 here";
$Example_string_PART_REMOVED = str_replace('11223344', '', $Example_string);

// will leave you with "REGISTER  here"

// finally - clean up potential double spaces, beginning spaces or end spaces that may have resulted from removing the unwanted string
$Example_string_COMPLETED = trim(str_replace('  ', ' ', $Example_string_PART_REMOVED));
// trim() will remove any potential leading and trailing spaces - the additional 'str_replace()' will remove any potential double spaces

// will leave you with "REGISTER here"

You can use str_replace(), which is defined as:

str_replace($search, $replace, $subject)

So you could write the code as:

$subject = 'REGISTER 11223344 here' ;
$search = '11223344' ;
$trimmed = str_replace($search, '', $subject) ;
echo $trimmed ;

If you need better matching via regular expressions you can use preg_replace().


If you're specifically targetting "11223344", then use str_replace:

// str_replace($search, $replace, $subject)
echo str_replace("11223344", "","REGISTER 11223344 here");

substr() is a built-in php function which returns part of a string. The function substr() will take a string as input, the index form where you want the string to be trimmed. and an optional parameter is the length of the substring. You can see proper documentation and example code on http://php.net/manual/en/function.substr.php

NOTE : index for a string starts with 0.


When you need rule based matching, you need to use regex

$string = "REGISTER 11223344 here";
preg_match("/(\d+)/", $string, $match);
$number = $match[1];

That will match the first set of numbers, so if you need to be more specific try:

$string = "REGISTER 11223344 here";
preg_match("/REGISTER (\d+) here/", $string, $match);
$number = $match[1];

I wanted to remove "www." from a href so I did this:

const str = "https://www.example.com/path";

str.split("www.").join("");

// https://example.com/path

assuming 11223344 is not constant

$string="REGISTER 11223344 here";
$s = explode(" ",$string);
unset($s[1]);
$s = implode(" ",$s);
print "$s\n";