如果有一个字符串,最大6位.可以输入1-6位.如果当前输入n位.那么n-1必须为字母或*号.如果不符合,则出错.另外正则表达式如果匹配上的话,可以得到匹配的位置吗.

解决方案 »

  1.   

    //一个汉字算几位?以下正则只针对单字节字符串
    ^[\x00-\xff]{1,4}[a-zA-Z*][\x00-\xff]$
      

  2.   

    //上面写的有点问题,修正如下:
    ^[\\x00-\\xff]|[\\x00-\\xff]{0,4}[a-zA-Z*][\\x00-\\xff]$
      

  3.   

    String s = "abc*ea";
    Pattern p = Pattern.compile("[a-zA-Z|\\*]{1,5}.");
    Matcher t = p.matcher(s);
    if(!t.matches()){
    while(t.find()){
    System.out.println(t.group()+":"+t.start());//start就是不符合的位置
    }
    }else{
    System.out.println(t.matches());
    }
      

  4.   

    谢谢楼上几位.
    leojay1(绯村剑心),我是想在JS里面做校验.
    CrazyGou() ,我现在能校验,但是得不到出错的位置.怎么才能在JS里做校验.又能得到出错的位置呢.
      

  5.   

    var destination = document.all.outboudDestination;
    var len=destination.value.length - 1; 
    var re = new RegExp("^[A-Za-z*]{"+len+"}",""); 
    var destinationChx = document.all.destinationChx;
      if(destinationChx.checked){
            if (destination.value.length > 3) {
                alert("Flight Destination should be 3 characters");
                destination.focus();
                return false;
            }
            
            if(destination.value.length > 0){
            if (re.test(destination.value) == false) {
                alert("Destination character (i + 1) should be letter or asterix");
                destination.focus();
                return false;
            }
            }
        }这是我现在的做法, 出错的位置还没有得到
      

  6.   

    if(destination.value.length > 0){
            var index=destination.value.search(re);
            if ( index == -1) {
                alert("Destination character (i + 1) should be letter or asterix");
                destination.focus();
                return false;
            }
            else{
                alert("position:"+index);
            }    }