Using str. partition() to get the part of a string before the first occurrence of a specific character. String partition() method return tuple.
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.
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"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With