Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove part of a string before a ":" in javascript?

Tags:

javascript

People also ask

How do you cut a string before?

Using str. partition() to get the part of a string before the first occurrence of a specific character. String partition() method return tuple.

How do I remove part of a string?

You can also remove a specified character or substring from a string by calling the String. Replace(String, String) method and specifying an empty string (String. Empty) as the replacement. The following example removes all commas from a string.

How do you cut a string in JavaScript?

The slice() method extracts a part of a string. The slice() method returns the extracted part in a new string. The slice() method does not change the original string. The start and end parameters specifies the part of the string to extract.


There is no need for jQuery here, regular JavaScript will do:

var str = "Abc: Lorem ipsum sit amet";
str = str.substring(str.indexOf(":") + 1);

Or, the .split() and .pop() version:

var str = "Abc: Lorem ipsum sit amet";
str = str.split(":").pop();

Or, the regex version (several variants of this):

var str = "Abc: Lorem ipsum sit amet";
str = /:(.+)/.exec(str)[1];

As a follow up to Nick's answer If your string contains multiple occurrences of : and you wish to only remove the substring before the first occurrence, then this is the method:

var str = "Abc:Lorem:ipsum:sit:amet";
arr = str.split(":");
arr.shift();
str = arr.join(":");
// str = "Lorem:ipsum:sit:amet"