Javascript 如何在javascript中的“:”之前删除部分字符串?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4092325/
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
How to remove part of a string before a ":" in javascript?
提问by Victor
If I have a string Abc: Lorem ipsum sit amet
, how can I use JavaScript/jQuery to remove the string before the :
including the :
. For example the above string will become: Lorem ipsum sit amet
.
如果我有一个字符串Abc: Lorem ipsum sit amet
,我如何使用 JavaScript/jQuery 在:
包含:
. 例如上面的字符串将变成:Lorem ipsum sit amet
.
回答by Nick Craver
There is no need for jQuery here, regular JavaScript will do:
这里不需要 jQuery,普通的 JavaScript 就可以了:
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];