javascript-js正则表达式-常用的正则表达式

js常用的正则表达式

1.匹配Email地址:

const emailRegex = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/;

2.匹配URL:

const urlRegex = /^(https?:\/\/)?([a-zA-Z0-9.-]+\.[a-zA-Z]{2,})(:[0-9]+)?(\/[^\s]*)?$/;

3.匹配日期(YYYY-MM-DD):

const dateRegex = /^\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01])$/;

4.匹配手机号码(简化版):

const phoneRegex = /^\d{11}$/;

5.匹配IP地址(IPv4):

const ipRegex = /^(\d{1,3}\.){3}\d{1,3}$/;

6.匹配数字:

const numberRegex = /^\d+$/;

7.匹配字母和数字:

const alphanumericRegex = /^[a-zA-Z0-9]+$/;

8.匹配至少包含一个大写字母、一个小写字母和一个数字的密码:

const passwordRegex = /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d).{8,}$/;

9.匹配HTML标签:

const htmlTagRegex = /<[^>]+>/g;

10.匹配多个重复字符(例如,连续的重复字母或数字):

const repeatedCharRegex = /(.)\1+/g;

11.匹配只包含字母(大小写不限)和空格的字符串:

const lettersAndSpacesRegex = /^[a-zA-Z\s]+$/;

12.匹配包含任意字符的非空字符串:

const nonEmptyStringRegex = /^.+$/;

13.匹配全是空白字符(空格、制表符、换行符等)的字符串:

const whitespaceOnlyRegex = /^\s*$/;

14.匹配手机号码(支持常见格式,如带区号、带空格等):

const phoneRegex = /^(?:\+?86)?\s*1\d{10}$/;

15.匹配邮政编码(支持5位和9位美国邮编):

const usZipCodeRegex = /^\d{5}(?:-\d{4})?$/;

16.匹配全是数字或者小数的字符串:

const numericOrDecimalRegex = /^(\d+|\d+\.\d+)$/;

17.匹配以特定字符串开头(不区分大小写):

const startsWithHelloRegex = /^hello/i;

18.匹配以特定字符串结尾(不区分大小写):

const endsWithWorldRegex = /world$/i;

19.提取字符串中的数字:

const extractNumbersRegex = /\d+/g;

20.匹配HTML中的图片标签:

const imgTagRegex = /<img[^>]+>/g;

21.匹配全是字母的单词(不区分大小写):

const wordRegex = /^[a-zA-Z]+$/;

22.匹配全是大写字母的单词:

const uppercaseWordRegex = /^[A-Z]+$/;

23.匹配全是小写字母的单词:

const lowercaseWordRegex = /^[a-z]+$/;

24.匹配16进制颜色代码:

const hexColorRegex = /^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$/;

25.匹配邮件中的用户名部分(@符号之前的部分):

const usernameFromEmailRegex = /^[a-zA-Z0-9._%+-]+/;

26.匹配连续重复的单词:

const repeatedWordsRegex = /\b(\w+)\s+\1\b/gi;

27.匹配整数或带正负号的浮点数:

const integerOrFloatRegex = /^-?\d+(\.\d+)?$/;

28.匹配日期和时间(YYYY-MM-DD HH:MM格式):

const dateTimeRegex = /^\d{4}-\d{2}-\d{2}\s+\d{2}:\d{2}$/;

29.匹配包含多个句子的段落(以句号、问号或感叹号结尾):

const paragraphRegex = /[^.!?]+[.!?]/g;

30.匹配JavaScript注释:

const jsCommentRegex = /\/\/.*|\/\*[\s\S]*\*\//g;

31.全是空白行的段落:

const emptyLinesRegex = /^\s*$/gm;

32.HTML注释:

const htmlCommentRegex = /<!--[\s\S]*?-->/g;

33.包含n个连续相同字符的单词:

const consecutiveCharactersRegex = /(\w)\1{2,}/g;

34.base64编码的字符串:

const base64Regex = /^[A-Za-z0-9+/=]+$/;

35.重复的HTML标签(例如,<p>...</p><p>...</p>):

const repeatedHtmlTagsRegex = /<(\w+)[\s\S]*?<\/\1>/g;

36.Unicode表情符号:

const emojiRegex = /[\u{1F600}-\u{1F64F}]/gu;

37.XML标签(支持带属性的标签):

const xmlTagRegex = /<([A-Za-z][A-Za-z0-9]*)\b[^>]*>(.*?)<\/\1>/g;

38.不包含某个单词的句子:

const sentenceWithoutWordRegex = /(?!\bword\b).*?[.!?]/g;

39.CSS中的类选择器:

const cssClassRegex = /\.[a-zA-Z][a-zA-Z0-9_-]*/g;

40.匹配JavaScript中的函数调用(支持多行):

const functionCallRegex = /\b\w+\s*\([^)]*\)[;\n]?/g;

41.匹配汉字:

const chineseCharacterRegex = /[\u4E00-\u9FFF]+/g;

42.匹配身份证:

const chineseIDCardRegex = /^[1-9]\d{5}(18|19|20)\d{2}(0[1-9]|1[0-2])(0[1-9]|[1-2]\d|3[0-1])\d{3}[\dX]$/i;

 

posted @ 2023-07-18 10:00  上官靖宇  阅读(344)  评论(0编辑  收藏  举报