2013-10-28 18 views

回答

3
var s = " xxxxxxx -errors follow: xxxxxxxxx failed validation xxxxx : yyyyyyyyyyyyyyyyy " 
s.substring(s.lastIndexOf(':')+1) 
3

您可以使用String.prototype.split()並得到最後一個結果數組中:

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

你可以使用正則表達式是這樣的:

/:\s*([^:]*)\s*$/ 

這將匹配後跟零文字:或更多空格字符,後面跟零個或多個任意字符,而不是:,在組1中捕獲,後跟零個或多個空白字符,結束的字符串。

例如:

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

可以使用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 
2
var str=" xxxxxxx -errors follow: xxxxxxxxx failed validation xxxxx : yyyyyyyyyyyyyyyyy " 

var arr=new Array(); arr=str.split(":"); 

var output=arr[arr.length-1]; 
1
var s=" xxxxxxx -errors follow: xxxxxxxxx failed validation xxxxx : yyyyyyyyyyyyyyyyy " 

s= s.substr(s.lastIndexOf(':')+1); 
相關問題