W3CAPI 在线教程 | 菜鸟教程_LOGO
文档目录
文档目录
我的书签
 

JavaScript

JavaScript 入门教程

JavaScript是一门Web编程语言,用来实现网页的交互功能,它和HTML、CSS共同组成了个Web开发的基础工具集合,也是前端开发者必备的技能;学习JavaScript教程可以了解它在网页开发中的所有特性和相关概念,让我们能够更加快速的去开发Web应用。

W3CAPI
1
2025-06-21 09:36:01

创建正则表达式

JavaScript:正则表达式

正则表达式

正则表达式(有时缩写为“regex”)是一种用于匹配字符串中字符组合的模式。

例如,正则表达式可用于搜索段落中包含单词"red"的所有文本行,并显示找到匹配项的这些行。

您也可将"red"替换为"green"。正则表达式常用于验证HTML表单字段中的邮箱、密码、用户名等内容的格式有效性。在JavaScript中,正则表达式同样属于对象类型。

创建正则表达式

构建正则表达式有两种方式。

1. 使用对象初始化器,如下所示:

var colorName = /Green/;。变量 colorName 赋值为正则表达式 /Green/

上述代码创建了一个名为colorName的新RegExp对象,并为其分配了模式Green。在此表示法中,正斜杠字符(/)用于标识模式的起始和结束位置。

2. 使用构造函数,如下所示:

正则表达式实例化:re = new RegExp("Green")

JavaScript:正则表达式模式

有时需使用多种模式匹配而非直接匹配。例如模式/xy*z/可匹配任意字符。

例如,正则表达式/bo*/在"A book"中匹配'boo',在"A beautiful river"中匹配'b',但在"A going concern"中不匹配任何内容。

使用test方法:

let TextStr = "The quick brown fox jumps over the lazy dog.";
let MatchRegex = /quick/;
let rexp = MatchRegex.test(TextStr); 
console.log(rexp); //true
MatchRegex = /Quick/;
rexp = MatchRegex.test(TextStr);
console.log(rexp); //false
执行一下

匹配字面量字符串

let TextStr = "The quick brown fox jumps over the lazy dog.";
let MatchRegex = /quick/; 
let rexp = MatchRegex.test(TextStr); 
console.log(rexp); //true
执行一下

匹配具有多种可能性的字面字符串:

let TextStr = "Cow is domestic animal.";
let DomRegexp = /Cow|cat|rose|dog/;
let rexp = DomRegexp.test(TextStr);
console.log(rexp); //true
执行一下

匹配时忽略大小写

let TextStr = "JavaScriptTutorial";
let IgCaseRegexp = /JavaScriptTutorial/i;
let rexp = IgCaseRegexp.test(TextStr);
console.log(rexp); //true
执行一下

提取匹配项

let TextStr = "Extract the 'zip' from this file.";
let codingRegexp = /zip/;
let rexp = TextStr.match(codingRegexp);
console.log(rexp); //["zip"]
执行一下

查找所有匹配项,而非仅首个

let TextStr = "The quick brown fox jumps over the lazy dog";
let MoreRegexp = /the/gi;
let rexp = TextStr.match(MoreRegexp);
console.log(rexp); //["The", "the"]
执行一下

通配符句点:匹配任意内容

let TextStr = "The final phases of the war!";
let WildRegexp = /.ar/;
let rexp = WildRegexp.test(TextStr);
console.log(rexp); //true
执行一下

匹配具有多种可能性的单个字符:使用字符集(如[aeiou])匹配单个字符,允许指定多个可能匹配项。

let TestStr = "The quick brown fox jumps over the lazy dog.";
let vowelRegexp = /[aeiou]/gi; 
let rexp = TestStr.match(vowelRegexp); 
console.log(rexp); //["e", "u", "i", "o", "o", "u", "o", "e", "e", "a", "o"]
执行一下

匹配字母表中的字母:

let TestStr = "Pack my box with five dozen liquor jugs.";
let alphabetRegexp = /[a-z]/gi; 
let rexp = TestStr.match(alphabetRegexp); 
console.log(rexp); // ["P", "a", "c", "k", "m", "y", "b", "o", "x", "w", "i", "t", "h", "f", "i", "v", "e", "d", "o", "z", "e", "n", "l", "i", "q", "u", "o", "r", "j", "u", "g", "s"]
执行一下

匹配数字与字母表中的字母:

let TestStr = "Python 3.8.0, documentation released on 14 October 2019.";
let my_Regexp = /[d-n0-9]/gi; 
let rexp = TestStr.match(my_Regexp); 
console.log(rexp); // ["h", "n", "3", "8", "0", "d", "m", "e", "n", "i", "n", "e", "l", "e", "e", "d", "n", "1", "4", "e", "2", "0", "1", "9"]
执行一下

匹配未指定的单个字符:

let TestStr = "The 4 books was on the table.";
let my_Regexp = /[^aeiou^0-9]/gi; 
let rexp = TestStr.match(my_Regexp); 
console.log(rexp); // ["T", "h", " ", " ", "b", "k", "s", " ", "w", "s", " ", "n", " ", "t", "h", " ", "t", "b", "l", "."]
执行一下

匹配出现一次或多次的字符:使用+符号

let TestStr = "Hippopotamus";
let my_Regexp = /p+/g; 
let rexp = TestStr.match(my_Regexp);
console.log(rexp); // ["pp", "p"]
执行一下

匹配出现零次或多次的字符:

let TestStr = "Huuuuuuuuuuuuuuuurrah!";
let my_Regexp = /Hu*/; 
let rexp= TestStr.match(my_Regexp);
console.log(rexp); // ["Huuuuuuuuuuuuuuuu"]
执行一下

使用惰性匹配查找字符

let textStr = "<em>Wind blow very fast</em>";
let my_Regexp = /<em>?/; 
let rexp = textStr.match(my_Regexp);
console.log(rexp); // ["<em>"]
执行一下

匹配字符串开头模式:

let TestStr = "Pack my box with five dozen liquor jugs.";
let my_Regexp = /^Pack/; 
let rexp = my_Regexp.test(TestStr);
console.log(rexp); // true
执行一下

匹配字符串结尾模式:

let TestStr = "Pack my box with five dozen liquor jugs";
let my_Regexp = /jugs$/; 
let rexp = my_Regexp.test(TestStr);
console.log(rexp); // true
执行一下

匹配字符串结尾模式:

let TestStr = "Pack my box with five dozen liquor jugs";
let my_Regexp = /jugs$/; 
let rexp = my_Regexp.test(TestStr);
console.log(rexp); // true
执行一下

匹配所有字母及数字:

let TestStr = "Pack my box with five dozen liquor jugs";
let my_Regexp = /\b/gi; 
let rexp = TestStr.match(my_Regexp).length;
console.log(rexp); // 16
执行一下

匹配除字母和数字外的所有字符

let TestStr = "Pack my box with five dozen liquor jugs";
let my_Regexp = /\W/g; 
let rexp = TestStr.match(my_Regexp).length;
console.log(rexp); // 7
执行一下

匹配指定用户名规则:

let TestStr = "TheQuickbrownFox";
let userRexp = /^[a-z]([0-9][0-9]+|[a-z]+\d*)$/i;
let rexp = userRexp.test(TestStr);
console.log(rexp); // true
执行一下

匹配空白字符:

let TestStr = "Pack my box with five dozen liquor jugs";
let countWSRexp = /\s/g; 
let rexp = TestStr.match(countWSRexp);
console.log(rexp)// [" ", " ", " ", " ", " ", " ", " "]
执行一下

匹配非空白字符:

let TestStr = "Pack my box with five dozen liquor jugs";
let countNWSRexp = /\S/g; 
let rexp = TestStr.match(countNWSRexp);
console.log(rexp)// ["P", "a", "c", "k", "m", "y", "b", "o", "x", "w", "i", "t", "h", "f", "i", "v", "e", "d", "o", "z", "e", "n", "l", "i", "q", "u", "o", "r", "j", "u", "g", "s"]
执行一下

指定匹配次数的上下限:

let TestStr = "Ok bye";
let my_Regex = /Ok{1,5}\sbye/; 
let rexp = my_Regex.test(TestStr)
console.log(rexp) // true
执行一下

仅指定最小匹配次数:

let TestStr = "Hauuurah";
let my_Regex = /Hau{3,}rah/; 
let rexp = my_Regex.test(TestStr);
console.log(rexp) // true
执行一下

指定精确匹配次数:

let TestStr = "Huuuuuurah";
let my_Regexp = /Hu{6}rah/; 
let rexp = my_Regexp.test(TestStr);
console.log(rexp) // true
执行一下

全有或全无检查

let TestStr = "Beautiful";
let my_Regexp = /Beauti?ful/; 
let rexp = my_Regexp.test(TestStr);
console.log(rexp) // true
执行一下

正向与负向先行断言:

let TestStr = "Skyrider";
let my_Regexp = /^(?=\w{6,})(?=\D*\d{2})/;
let rexp = my_Regexp.test(TestStr);
console.log(rexp) // false
执行一下

检查字符混合分组:

let TestStr = "JavaScript Tutorial";
let myRegexp = /(PHP|JavaScript).*Tutorial/;
let rexp = myRegexp.test(TestStr);
console.log(rexp) // true
执行一下

使用捕获组复用模式:

let TestStr = "15 15 15";
let my_Regex = /^(\d+)\s\1\s\1$/;
let rexp = my_Regex.test(TestStr);
console.log(rexp) // true
执行一下

使用捕获组执行搜索与替换:

let TextStr = "This is red flower.";
let Org_Regexp = /red/; 
let replace_Regexp = "yellow"; 
let rexp = TextStr.replace(Org_Regexp, replace_Regexp);
console.log(rexp) // "This is yellow flower."
执行一下

移除开头和结尾处的空白符

let TestStr = "   JavaScript, Tutorial!  ";
let my_Regexp = /^\s+|\s+$/g; 
let rexp = TestStr.replace(my_Regexp, ""); 
console.log(rexp) // "JavaScript, Tutorial!"
执行一下

下表提供了可在正则表达式中使用的特殊模式匹配字符的完整列表及功能说明。

CharacterMeaning
\表示下一个字符具有特殊含义且不应按字面解析
例如,/d/ 匹配字符'd'。通过在d前添加反斜杠形成/\d/,该字符将具有特殊含义,表示匹配所有数字字符。
-or-
表示下一个字符不具有特殊含义,应按照字面意义进行解析。
^匹配字符串或行的开头。
例如 /^A/ 不会匹配 "about Articles" 中的 'A',但会匹配 "Articles of life" 中的 'A'。
$匹配字符串或行尾。
例如,/e$/ 正则表达式不匹配 "exact" 中的字符 e,但会匹配 "w3capie" 中的字符 e。

*匹配前一个字符零次或多次。
例如,/bo*/在"A bootable usb"中匹配'boo',在"A beautiful mind"中匹配'b',但在"A going concern"中无匹配项。
+匹配前一个字符一次或多次。
例如,/a+/ 会匹配 "Daniel" 中的 'a' 以及 "Daaam" 中的所有a。
?匹配前一个字符0次或1次。
例如,/r?eu?/ 会匹配"w3recapi"中的're'和"europe"中的'eu'。
.小数点可匹配除换行符外的任意单个字符。
例如,正则表达式/.n/会匹配"an orange is on the table"中的"an"和"on"。
(x)匹配'x'并捕获该字符。例如,/(go)/ 在"go there"中匹配并捕获'go'。
x|y匹配'x'或'y'。
例如,正则表达式/green|red/会在"green color"中匹配到'green',在"blue color"中匹配到'blue'。
{n}精确匹配前面字符恰好出现n次(n为正整数)。
例如,正则表达式/a{2}/不会匹配"dam"中的a字符,但会匹配"daam"中的所有a字符,以及"daaam"中的前两个a字符。
{n,}匹配前面字符至少出现n(一个正整数)次的情况。
例如,正则表达式/a{2,}/不会匹配"dam"中的单个a,但会匹配"daam"中的所有a,以及"daaam"中的前两个a。
{n,m}匹配前面字符至少n次且至多m次(n和m为正整数)。
例如,/a{1,3}/在"dom"中不匹配任何内容;在"dam"中匹配一个a;在"daam"中匹配前两个a;在"daaaaaam"中匹配前三个a。注意当匹配"daaaaaam"时,虽然原始字符串包含更多连续的a,但由于m的最大值为3,实际匹配结果为"aaa"。
[xyz]匹配字符集中的任意一个字符,通过连字符可指定连续字符范围。
例如,[uvwxyz]等同于[u-z]。在"yellow"中匹配'y',在"blue"中匹配'u'。
[^xyz]匹配未包含在方括号内的任意字符,使用连字符可指定字符范围范围。
例如,[^wxyz]等同于[^w-z]。它们会匹配"blue"中的'b'及"specify"中的's'。
[\b]匹配退格符。
\b匹配单词边界(单词字符与非单词字符之间的位置),例如空格。
例如,/\bs\w/ 匹配 "sooner" 中的 'so'
\B匹配位于单词字符与非单词字符边界之后的位置(非单词边界)。
例如,/\w\Bn/在"sooner"中匹配到'on'。
\cX匹配字符串中的控制字符(X)。例如,/\cM/ 会匹配字符串中的 control-M 控制字符。
\d匹配任意数字字符。等价于[0-9]。
例如,/\d/或/[0-9]/会在"E2 means second example."中匹配到'2'。
\D匹配任意非数字字符。等价于[^0-9]。
例如,正则表达式/\D/或/[^0-9]/会在"E2 means second example."中匹配'E'字符。
\f匹配换页符
\n匹配换行符
\r匹配回车符。
\s匹配任意空白字符(包括制表符、换行符、回车符、换页符、垂直制表符)。对应字符集为[ \t\n\r\f\v]。
例如,/\s\w*/ 在"An apple."中匹配到' apple'。
\S匹配任意非空白字符。等同于 [^ \f\n\r\t\v]。
例如,/\S\w*/ 在“An apple”中匹配到‘An’
\t匹配标签页
\v匹配垂直制表符。
\w匹配任意单词字符(字母、数字),包括下划线。等价于字符集[A-Za-z0-9_]。
例如,/\w/ 匹配"green"中的'g'、"12.86"中的'8'以及"3G"中的'3'。
\W匹配任意非单词字符,等效于[^A-Za-z0-9_]。
例如,/\W/或/[^$A−Za−z0−9]/ 会匹配"150"中的'$'字符
\n其中n为正整数。反向引用正则表达式中第n个括号匹配的最后一个子字符串。
例如,/red(,)\sgreen\1/ 正则表达式在"red, green, white, black."中匹配'red, green',其中捕获组(,)通过\1实现反向引用。
\ooctal
八进制转义值允许在正则表达式中嵌入ASCII编码。
\xhex十六进制转义值允许在正则表达式中嵌入ASCII编码。

上一篇:JavaScript 对象 Object - 属性与方法
下一篇:JavaScript正则表达式对象:属性与方法

相关提问
敬请期待