abcdefg
要求截取倒数两个字符,也就是“fg”.
要求截取倒数两个字符,也就是“fg”.
以下是几种实现方式:
<script>
string="abcdefg"
alert(string.substring(string.length-2,string.length))
</script>
string="abcdefg"
alert(string.substring(string.length-2,string.length))
</script>
<script>
alert("abcdefg".match(/.*(.{2})/)[1])
</script>
alert("abcdefg".match(/.*(.{2})/)[1])
</script>
<script>
alert("abcdefg".match(/.{2}$/))
</script>
alert("abcdefg".match(/.{2}$/))
</script>
<script>
alert("abcdefg".slice(-2))
</script>
alert("abcdefg".slice(-2))
</script>
本文转自 牛海彬 51CTO博客,原文链接:http://blog.51cto.com/newhappy/76856,如需转载请自行联系原作者