Home >Web Front-end >JS Tutorial >Compare the differences between regular expressions in JavaScript and Java
This article mainly introduces the difference between JavaScript and Java regular expression writing. Friends in need can refer to
Js verification writing method: (escape character\)
var str = "待验证文本"; var regular = new RegExp(/这里是正则表达式/); if (regular.test(str)) { console.log("符合条件"); } else { console.log("不符合条件"); } //或者 var str = "待验证文本"; if (/这里是正则表达式/.test(str)) { console.log("符合条件"); } else { console.log("不符合条件"); }
Java verification writing method: (escape character \\)
String str = "待验证文本"; String regular = "这里是正则表达式"; if (Pattern.compile(regular).matcher(str).matches()) { System.out.println("符合条件"); } else { System.out.println("不符合条件"); } //或者 String str = "待验证文本"; if (str.matches("这里是正则表达式")) { System.out.println("符合条件"); } else { System.out.println("不符合条件"); }
Some commonly used regular expressions
验证只能含有6-16位大小写字母、数字、下划线: ^[\w]{6,16}$ 验证字符串是否全是空格组成: ^[\s]+$ 验证只能含有中文、大小写字母、数字、下划线(不含中文符号): ^[\u4E00-\u9FA5\w]+$ 验证邮箱格式: ^[\w-]+(\.[\w-]+)*@([\w-]+\.)+[a-zA-Z]+$ 验证只能含有大小写字母和数字 ^[a-zA-Z0-9]+$
The above is the detailed content of Compare the differences between regular expressions in JavaScript and Java. For more information, please follow other related articles on the PHP Chinese website!