javascript从右边截取指定字符串的三种实现方法

内容摘要
截取abcdefg右边的fg
方法一
<script> string="abcdefg" alert(string.substring(string.length-2,string.length)) </script>
方法2
<script> alert("abcdefg".match(/.*
文章正文

截取abcdefg右边的fg

方法一

<script>
string="abcdefg"
alert(string.substring(string.length-2,string.length))
</script>


方法2

<script>
alert("abcdefg".match(/.*(.{2})/)[1])
</script>

<script>
alert("abcdefg".match(/.{2}$/))
</script>


方法3

<script>
alert("abcdefg".slice(-2))  //推荐这个,比较简单,-2表示取右边两个字符
</script> 


代码注释

作者:喵哥笔记

IDC笔记

学的不仅是技术,更是梦想!