日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

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

發(fā)布時(shí)間:2025/3/21 编程问答 10 豆豆
生活随笔 收集整理的這篇文章主要介紹了 正则表达式在js和java中的使用 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

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

<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;責(zé)任編碼規(guī)則為:CV+5位(數(shù)字或大寫字母)&nbsp;&nbsp;</span>


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

? ?.)使用RegExp能動(dòng)態(tài)的設(shè)置正則表達(dá)式

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

uiTips.alert("險(xiǎn)種編碼規(guī)則:PL+2位產(chǎn)品大類編碼+5位(數(shù)字或大寫字母)");?

return;

? ? }

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

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

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

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

? ? 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("日期格式錯(cuò)誤!");

return;

}

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

? ? //分組的索引值是從1開始的,所以取第一個(gè)分組的方法是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)驗(yàn)證字符串的格式是否正確

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

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

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

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

//驗(yàn)證郵政編碼 ?

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

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

//驗(yàn)證電話號(hào)碼 ?

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

getDate("Nov 10,2009");

//驗(yàn)證***:判斷一個(gè)字符串是不是***號(hào)碼,即是否是15或18位數(shù)字。 ?

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

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

charReplace();

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

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

validateEmail("luosijin123@163.com");

}


轉(zhuǎn)載于:https://blog.51cto.com/6817977/1694660

總結(jié)

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

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。