请教replace使用方法

时间:2021-09-02 21:23:51
存在一个input
当中的字符串为:
abcd:123
其中的:是在中文状态下输入的全角字符

请问,我如何将这个字串中所有为":"的字符替换为":"

谢谢

14 个解决方案

#1


abcd:123
abcd.replace(":",":")

#2


str.replace(/:/g, ":");  //楼主的只能替换一个

#3


这个我使用了,但并不产生替换呀
我的代码如下,请大侠指点:
<input onkeydown="event.srcElement.value.replace(":",":")" value="abc:123">

#4


var newstring = str.replace(/:/g, ":"); 才对,另谢谢钻石老大补充

#5


老大别走!

如果我要先遍历所有字符中是否存在全角字符,比如:"A123B456:789"如果是全角的,就改为相应的英文状态输入的字符,应该如何写程序呢

#6


<input onblur="this.value=meizz(this.value)">
<SCRIPT LANGUAGE="JavaScript">
<!--
function meizz(str)
{
  var tmp = '';
  for(var i=0;i<str.length;i++)
  {
    tmp += String.fromCharCode(str.charCodeAt(i)-65248)
  }
  return tmp
}
alert(meizz('abcdefghijklmnopqrstuvwxyz1234567890'));
alert(meizz('~`!@#$%^&*()_+|-={}[]:";<>?,./\''));
//-->
</SCRIPT>

#7


收藏一下,65248 是怎么出来的,有相关资料吗

#8


65248是utf-8中全角英数和对应半角英数内码的差值(排列顺序是一样的,所以计算差值就可以了)

#9


mark

#10


/**//// <summary>
        /// 转全角的函数(SBC case)
        /// </summary>
        /// <param name="input">任意字符串</param>
        /// <returns>全角字符串</returns>
        ///<remarks>
        ///全角空格为12288,半角空格为32
        ///其他字符半角(33-126)与全角(65281-65374)的对应关系是:均相差65248
        ///</remarks>        
        public string ToSBC(string input)
        {
            //半角转全角:
            char[] c=input.ToCharArray();
            for (int i = 0; i < c.Length; i++)
            {
                if (c[i]==32)
                {
                    c[i]=(char)12288;
                    continue;
                }
                if (c[i]<127)
                    c[i]=(char)(c[i]+65248);
            }
            return new string(c);                
        }


        /**//// <summary>
        /// 转半角的函数(DBC case)
        /// </summary>
        /// <param name="input">任意字符串</param>
        /// <returns>半角字符串</returns>
        ///<remarks>
        ///全角空格为12288,半角空格为32
        ///其他字符半角(33-126)与全角(65281-65374)的对应关系是:均相差65248
        ///</remarks>
        public string ToDBC(string input)
        {    
            char[] c=input.ToCharArray();
            for (int i = 0; i < c.Length; i++)
            {
                if (c[i]==12288)
                {
                    c[i]= (char)32;
                    continue;
                }
                if (c[i]>65280 && c[i]<65375)
                    c[i]=(char)(c[i]-65248);
            }    
            return new string(c);
        }

#11


<input onblur="this.value=hotooReplace(this.value)">
<script language="JavaScript">
<!--
function hotooReplace( as ) {
   a = new Array('a', 'b', 'c', 'd', 'e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','A', 'B', 'C', 'D', 'E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','1','2','3','4','5','6','7','8','9','0','~','`','!','@','#','$','%','^','&','*','(',')','_','+','|','-','=','{','}','[',']',':','"',';','<','>','?',',','.','/','\',''');

   b = new Array('a', 'b', 'c', 'd', 'e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','x','A', 'B', 'C', 'D', 'E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','1','2','3','4','5','6','7','8','9','0','~','`','!','@','#','$','%','^','&','*','(',')','_','+','|','-','=','{','}','[',']',':','"',';','<','>','?',',','.','\/','\\','\'');

   for (var i = 0; i < a.length;i++) {
      as = as.replace(eval('new RegExp(/' + a[i] + '/g)'), b[i]) ;
   }
   return as ;
}

alert(hotooReplace('abbaab')) ;
//-->
</script>

#12


在我的页面中(win2003server+ie6.0.3790)使用了梅老大的代码可是转换后显示出来的却是:&#3233;&#2929;&#2642;&#2355;&#2068;&#1781;&#1494;&#1207;

何故呀??再请指点!!

#13


梅老大的程序是针对所有字符都是全角
你要适当加入判断

#14


"XXX"=replace("X'XX","'", ":"); 

#1


abcd:123
abcd.replace(":",":")

#2


str.replace(/:/g, ":");  //楼主的只能替换一个

#3


这个我使用了,但并不产生替换呀
我的代码如下,请大侠指点:
<input onkeydown="event.srcElement.value.replace(":",":")" value="abc:123">

#4


var newstring = str.replace(/:/g, ":"); 才对,另谢谢钻石老大补充

#5


老大别走!

如果我要先遍历所有字符中是否存在全角字符,比如:"A123B456:789"如果是全角的,就改为相应的英文状态输入的字符,应该如何写程序呢

#6


<input onblur="this.value=meizz(this.value)">
<SCRIPT LANGUAGE="JavaScript">
<!--
function meizz(str)
{
  var tmp = '';
  for(var i=0;i<str.length;i++)
  {
    tmp += String.fromCharCode(str.charCodeAt(i)-65248)
  }
  return tmp
}
alert(meizz('abcdefghijklmnopqrstuvwxyz1234567890'));
alert(meizz('~`!@#$%^&*()_+|-={}[]:";<>?,./\''));
//-->
</SCRIPT>

#7


收藏一下,65248 是怎么出来的,有相关资料吗

#8


65248是utf-8中全角英数和对应半角英数内码的差值(排列顺序是一样的,所以计算差值就可以了)

#9


mark

#10


/**//// <summary>
        /// 转全角的函数(SBC case)
        /// </summary>
        /// <param name="input">任意字符串</param>
        /// <returns>全角字符串</returns>
        ///<remarks>
        ///全角空格为12288,半角空格为32
        ///其他字符半角(33-126)与全角(65281-65374)的对应关系是:均相差65248
        ///</remarks>        
        public string ToSBC(string input)
        {
            //半角转全角:
            char[] c=input.ToCharArray();
            for (int i = 0; i < c.Length; i++)
            {
                if (c[i]==32)
                {
                    c[i]=(char)12288;
                    continue;
                }
                if (c[i]<127)
                    c[i]=(char)(c[i]+65248);
            }
            return new string(c);                
        }


        /**//// <summary>
        /// 转半角的函数(DBC case)
        /// </summary>
        /// <param name="input">任意字符串</param>
        /// <returns>半角字符串</returns>
        ///<remarks>
        ///全角空格为12288,半角空格为32
        ///其他字符半角(33-126)与全角(65281-65374)的对应关系是:均相差65248
        ///</remarks>
        public string ToDBC(string input)
        {    
            char[] c=input.ToCharArray();
            for (int i = 0; i < c.Length; i++)
            {
                if (c[i]==12288)
                {
                    c[i]= (char)32;
                    continue;
                }
                if (c[i]>65280 && c[i]<65375)
                    c[i]=(char)(c[i]-65248);
            }    
            return new string(c);
        }

#11


<input onblur="this.value=hotooReplace(this.value)">
<script language="JavaScript">
<!--
function hotooReplace( as ) {
   a = new Array('a', 'b', 'c', 'd', 'e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','A', 'B', 'C', 'D', 'E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','1','2','3','4','5','6','7','8','9','0','~','`','!','@','#','$','%','^','&','*','(',')','_','+','|','-','=','{','}','[',']',':','"',';','<','>','?',',','.','/','\',''');

   b = new Array('a', 'b', 'c', 'd', 'e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','x','A', 'B', 'C', 'D', 'E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','1','2','3','4','5','6','7','8','9','0','~','`','!','@','#','$','%','^','&','*','(',')','_','+','|','-','=','{','}','[',']',':','"',';','<','>','?',',','.','\/','\\','\'');

   for (var i = 0; i < a.length;i++) {
      as = as.replace(eval('new RegExp(/' + a[i] + '/g)'), b[i]) ;
   }
   return as ;
}

alert(hotooReplace('abbaab')) ;
//-->
</script>

#12


在我的页面中(win2003server+ie6.0.3790)使用了梅老大的代码可是转换后显示出来的却是:&#3233;&#2929;&#2642;&#2355;&#2068;&#1781;&#1494;&#1207;

何故呀??再请指点!!

#13


梅老大的程序是针对所有字符都是全角
你要适当加入判断

#14


"XXX"=replace("X'XX","'", ":");