Home > Web Front-end > JS Tutorial > Compare the differences between regular expressions in JavaScript and Java

Compare the differences between regular expressions in JavaScript and Java

巴扎黑
Release: 2017-08-18 09:57:16
Original
1552 people have browsed it

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("不符合条件");
}
Copy after login

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("不符合条件");
}
Copy after login

Some commonly used regular expressions


验证只能含有6-16位大小写字母、数字、下划线:
^[\w]{6,16}$
验证字符串是否全是空格组成:
^[\s]+$
验证只能含有中文、大小写字母、数字、下划线(不含中文符号):
^[\u4E00-\u9FA5\w]+$
验证邮箱格式:
^[\w-]+(\.[\w-]+)*@([\w-]+\.)+[a-zA-Z]+$
验证只能含有大小写字母和数字
^[a-zA-Z0-9]+$
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template