Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex remove everything after : (including :)

Hey everyone I have a string like: Foundation: 100

I want to use JS regex to remove everything after : including (:)

thanks for any help!

like image 426
m_gunns Avatar asked Aug 25 '12 15:08

m_gunns


People also ask

How do you delete everything after regex?

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.

What is $& in regex?

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.

How do you remove everything from a string after a character?

Use the String. slice() method to remove everything after a specific character, e.g. const removed = str. slice(0, str. indexOf('[')); .

How do I remove a specific character from a string in regex?

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]+", "")


2 Answers

var text = "Foundation: 100";
text = text.replace(/:.*$/, "");
console.log(text);
like image 199
Mihai Iorga Avatar answered Oct 08 '22 00:10

Mihai Iorga


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)
like image 31
jon Avatar answered Oct 07 '22 23:10

jon