使用Javascript删除字符串的最后一个字符 [英] Delete Last Char of String with Javascript

查看:105
本文介绍了使用Javascript删除字符串的最后一个字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些带有一些字符的 DIV 。如何在每次点击 DIV 本身时删除文本中的最后一个字符?

I have a DIV with some characters. How can I remove the last character from the text with each click on the DIV itself?

推荐答案

删除第一个字符



Removing First Character

​$("div").on("click", function(){
    $(this).text(function(index, text){
        return text.replace(/^.(\s+)?/, '');
    });
});​​​​​​​​​​​​



删除最后字符



Removing Last Character

$("div").on("click", function(){
    $(this).text(function(index, text){
        return text.replace(/(\s+)?.$/, '');
    });
});



删除特定字符



Removing a Specific Char

$("div").on("click", function(){
    $(this).text(function(index, text){
        return text.replace(/r/gi, '');
    });
});

查看每个在线的示例: http://jsfiddle.net/Xcn6s/

See an example of each online at: http://jsfiddle.net/Xcn6s/

这篇关于使用Javascript删除字符串的最后一个字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆