<script>
string="abcdefg"
alert(string.substring(string.length-2,string.length))
</script>
<script>
alert("abcdefg".match(/.*(.{2})/)[1])
</script>
<script>
alert("abcdefg".match(/.{2}$/))
</script>
<script>
alert("abcdefg".slice(-2))
</script>
本文介绍了使用JavaScript通过不同方法截取字符串末尾两个字符的具体实现,包括使用substring、正则表达式match、以及slice方法等。
<script>
string="abcdefg"
alert(string.substring(string.length-2,string.length))
</script>
<script>
alert("abcdefg".match(/.*(.{2})/)[1])
</script>
<script>
alert("abcdefg".match(/.{2}$/))
</script>
<script>
alert("abcdefg".slice(-2))
</script>
1988

被折叠的 条评论
为什么被折叠?