2011-12-15 27 views
0

任何人都可以帮助我格式化下面的字符串,以显示最终结果为“马克,安东尼”我一直使用javascript中的子字符串进行格式化,并且我们有任何类似的子序列来使这些格式在单行代码中?javascript中的子字符串/子序列

var res="undefinedMark, Anthony," 
var final = res.substring(9, res.length); // removes undefined 
var finalOP = final.substring(0, final.length - 9); // removes the final , 

感谢

回答

1

你可以使用String.replace方法:

var final = res.replace(/undefined/,'') //=>replace undefined with '' 
       .replace(/,$/,'');  //=>replace the last ',' 
+0

感谢您的帮助! – pal 2011-12-15 09:29:24