Hey everyone I have a string like: Foundation: 100
I want to use JS regex to remove everything after :
including (:)
thanks for any help!
Regex Replace We can also call the string replace method with a regex to remove the part of the string after a given character. The /\?. */ regex matches everything from the question to the end of the string. Since we passed in an empty string as the 2nd argument, all of that will be replaced by an empty string.
You get the same value because you enclosed the whole pattern in a capturing group. The $& is a backreference to the whole match, while $1 is a backreference to the submatch captured with capturing group 1. See MDN String#replace() reference: $& Inserts the matched substring.
Use the String. slice() method to remove everything after a specific character, e.g. const removed = str. slice(0, str. indexOf('[')); .
If you are having a string with special characters and want's to remove/replace them then you can use regex for that. Use this code: Regex. Replace(your String, @"[^0-9a-zA-Z]+", "")
var text = "Foundation: 100";
text = text.replace(/:.*$/, "");
console.log(text);
For your example you could call .split(":")
on your string and then select the first array element using "[0]"
var whole_text = "Foundation: 100";
var wanted_text = whole_text.split(":")[0]
console.log(wanted_text)
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