abcdefg
要求截取倒数两个字符,也就是“fg”.
以下是几种实现方式:
<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等方法,并附带了具体的代码示例。
195

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



