Javascript 如何提取以“开始”开头的字符串:&引用;从另一根绳子的末端?

Javascript 如何提取以“开始”开头的字符串:&引用;从另一根绳子的末端?,javascript,Javascript,我有如下内容的javascript字符串: " xxxxxxx -errors follow: xxxxxxxxx failed validation xxxxx : yyyyyyyyyyyyyyyyy " /:\s*([^:]*)\s*$/ 如何从字符串中提取YYYY?注意:我想获取最后一个“:”和字符串末尾之间的文本。您可以使用string.prototype.split()获取结果数组中的最后一个: var a = " xxxxxxx -errors follow: xxxxxxxx

我有如下内容的javascript字符串:

" xxxxxxx -errors follow: xxxxxxxxx failed validation xxxxx : yyyyyyyyyyyyyyyyy " 
/:\s*([^:]*)\s*$/

如何从字符串中提取YYYY?注意:我想获取最后一个“:”和字符串末尾之间的文本。

您可以使用
string.prototype.split()
获取结果数组中的最后一个:

var a = " xxxxxxx -errors follow: xxxxxxxxx failed validation xxxxx : yyyyyyyyyyyyyyyyy ".split(':');
console.log(a[a.length - 1]); // " yyyyyyyyyyyyyyyyy "

您可以使用如下正则表达式:

" xxxxxxx -errors follow: xxxxxxxxx failed validation xxxxx : yyyyyyyyyyyyyyyyy " 
/:\s*([^:]*)\s*$/
这将匹配组1中捕获的文本
,后跟零个或多个空白字符,后跟零个或多个除
以外的任何字符,后跟零个或多个空白字符和字符串结尾

例如:

var input = " xxxxxxx -errors follow: xxxxxxxxx failed validation xxxxx : yyyyyyyyyyyyyyyyy ";
var output = input.match(/:\s*([^:]*)\s*$/)[1];
console.log(output); // "yyyyyyyyyyyyyyyyy"

您可以使用
string.lastIndexOf()
方法:

var text = " xxxxxxx -errors follow: xxxxxxxxx failed validation xxxxx : yyyyyyyyyyyyyyyyy ";
var index = text.lastIndexOf(":");
var result = text.substring(index + 1); // + 1 to start after the colon
console.log(result); // yyyyyyyyyyyyyyyyy 
var s=" xxxxxxx -errors follow: xxxxxxxxx failed validation xxxxx : yyyyyyyyyyyyyyyyy "

s= s.substr(s.lastIndexOf(':')+1);