判断一个字符串中是否包含除0-9以外的其他字符。

解决方案 »

  1.   


    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
    <HTML>
    <HEAD>
    <TITLE> New Document </TITLE>
    <META NAME="Generator" CONTENT="EditPlus">
    <META NAME="Author" CONTENT="">
    <META NAME="Keywords" CONTENT="">
    <META NAME="Description" CONTENT="">
    </HEAD>
    <script>
    alert(/[^0-9]+/.test("125a"));
    </script>
    <BODY></BODY>
    </HTML>
      

  2.   

    pid = pid.replaceAll("^([0-9]*$)","");
    为什么测试了一下不起作用啊,是java代码
      

  3.   

    /**
     * regular Expression test
     * @author WangYanCheng
     *
     */
    public class FileTest {
        /**
         * @param args args
         * @throws Exception exception
         */
        public static void main(String[] args) throws Exception {
            String str = new String("www.csdn.net3214www.csdn.net");
            str = str.replaceAll("\\d", "要替换的字符");
    System.out.println(str);
        }
    }
      

  4.   

    对不起大家,是我没说清楚,是这样的,现在有一个url请求参数pid,现在日志里有好多500错,原因是pid参数中有好多非法的字符,如&、%之类的,而程序希望pid参数只能是纯数字的,所以会报错
    现在希望把pid参数中除数字以外的其他字符过滤掉,就这么简单,我用大家提供的正则表达式好像都replace不掉非法字符
      

  5.   

    答:楼主试试这个呢:pid = pid.replaceAll("[^0-9]+)","");  
      

  6.   

    答:对不起,多打了个),应为:
    楼主试试这个呢:pid = pid.replaceAll("[^0-9]+","");   
      

  7.   

    package com.sytdc.cxl;import java.util.regex.*;public class MatcherTest { public static void main(String args[]){
    boolean b;
    b = IsDigit("rf12548");
    if(b){
    System.out.println("数字");
    }else{
    System.out.println("含有非数字");
    }

    }

    public static boolean IsDigit(String str){

    boolean b=false;
    b = Pattern.matches("[0-9]+", str);
    if(b){
    return true;
    }
    return false;
    }

    }