javascript 正则表达式删除之后的所有内容:(包括:)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12123085/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 15:22:54  来源:igfitidea点击:

Regex remove everything after : (including :)

javascriptregex

提问by m_gunns

Hey everyone I have a string like: "Foundation: 100"

大家好,我有一个字符串:“Foundation: 100”

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

我想使用 JS 正则表达式删除以下所有内容:包括 (:)

thanks for any help!

谢谢你的帮助!

回答by Mihai Iorga

var text = "Foundation: 100";
text = text.replace(/:.*$/,"");
console.log(text);

回答by jon

For your example you could call .split(":")on your string and then select the first array element using "[0]"

对于您的示例,您可以调用.split(":")您的字符串,然后使用"[0]"

var whole_text = "Foundation: 100";
var wanted_text = whole_text.split(":")[0]
console.log(wanted_text)