日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

正则表达式在js和java中的使用

發布時間:2025/3/21 20 豆豆
生活随笔 收集整理的這篇文章主要介紹了 正则表达式在js和java中的使用 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1)angularjs中如何使用正則表達式

<input type="text" ?ng-model="dutyCode" ng-focus="showDutyCodeTips = true"?

? ? ? ? ng-blur="showDutyCodeTips = false" ng-pattern="/^CV[0-9A-Z]{5}$/" >

? ?<span ng-init="showDutyCodeTips = false" ng-show="showDutyCodeTips"?

? ? ? ? ? ? ? style="color:red">&nbsp;&nbsp;責任編碼規則為:CV+5位(數字或大寫字母)&nbsp;&nbsp;</span>


2)js中如何使用正則表達式

? ?.)使用RegExp能動態的設置正則表達式

? ? if(! new RegExp("PL" + param.productClass + "[0-9A-Z]{5}").test(param.planCode)){

uiTips.alert("險種編碼規則:PL+2位產品大類編碼+5位(數字或大寫字母)");?

return;

? ? }

? ?.) 這樣直接使用正則表達式(表達式不能拼湊)

? ? ? ? var reg =/^PL03[0-9A-Z]{5}$/;

? ? ? ? console.log(reg.test("PL03AAAAA"));

3)java代碼中使用正則表達式

? ? String postfix = "abcde"

? ? return Pattern.compile("[0-9A-Z]{5}").matcher(postfix).matches();

??

public static void charReplace() {

String regex = "a+";

Pattern pattern = Pattern.compile(regex);

Matcher matcher = pattern.matcher("okaaaa LetmeAseeaaa aa booa");

String s = matcher.replaceAll("A");

System.out.println(s);

}


public static void getDate(String str) {

String regEx = "([a-zA-Z]+)|//s+[0-9]{1,2},//s*[0-9]{4}";

Pattern pattern = Pattern.compile(regEx);

Matcher matcher = pattern.matcher(str);

if (!matcher.find()) {

System.out.println("日期格式錯誤!");

return;

}

System.out.println(matcher.group(1));?

? ? //分組的索引值是從1開始的,所以取第一個分組的方法是m.group(1)而不是m.group(0)。 ?

}



public static void getString(String str) {

String regex = ".+/(.+)$";

Pattern pattern = Pattern.compile(regex);

Matcher matcher = pattern.matcher(str);

if (!matcher.find()) {

System.out.println("文件路徑格式不正確!");

return;

}

System.out.println(matcher.group(1));

}

public static void getChinese(String str) {

String regex = "[\u4E00-\u9FFF]+";//[//u4E00-//u9FFF]為漢字 ??

Pattern pattern = Pattern.compile(regex);

Matcher matcher = pattern.matcher(str);

StringBuffer sb = new StringBuffer();

while (matcher.find()) {

String termp = matcher.group();

System.out.println(termp);

sb.append(termp);

}

System.out.println(sb);

}


public static void validateEmail(String email) {

String regex = "[0-9a-zA-Z]+@[0-9a-zA-Z]+//.[0-9a-zA-Z]+";

Pattern pattern = Pattern.compile(regex);

Matcher matcher = pattern.matcher(email);

if (matcher.matches()) {

System.out.println("這是合法的Email");

} else {

System.out.println("這是非法的Email");

}

}

? ? ?

public static void main(String[] args) {

//1)驗證字符串的格式是否正確

Pattern pattern = Pattern.compile("b*g");

Matcher matcher = pattern.matcher("bbg");

//System.out.println(matcher.matches());

//System.out.println(pattern.matches("b*g", "bbg"));

//驗證郵政編碼 ?

//System.out.println(pattern.matches("[0-9]{6}", "200038"));

//System.out.println(pattern.matches("\\d{6}", "200038"));

//驗證電話號碼 ?

System.out.println(pattern.matches("[0-9]{3,4}-?[0-9]+", "02178989799"));

getDate("Nov 10,2009");

//驗證***:判斷一個字符串是不是***號碼,即是否是15或18位數字。 ?

System.out.println(pattern.matches("^\\d{15}|\\d{18}$", "123456789009876"));

//2)查詢字符串中滿足條件的子串,并將其替換掉

charReplace();

//3)提前匹配到的子串

getChinese("welcome to china,江西奉新,welcome,你!");

validateEmail("luosijin123@163.com");

}


轉載于:https://blog.51cto.com/6817977/1694660

總結

以上是生活随笔為你收集整理的正则表达式在js和java中的使用的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。