typescript 中的正则表达式
regexp
是正则表达式,用于使用 typescript 匹配目标字符串中的一组固定字符。
regexp
对象有许多属性和方法。模式匹配时,每个属性都有不同的选项。
在 typescript 中使用正则表达式
regex 可以通过两种方式在 typescript 中实现。其中之一是分配正则表达式文字类型 regexp
。
const rexp : regexp = /[a-c]/g;
另一种表达正则表达式的方法是通过 regexp
构造函数。
const rexp : regexp = new regexp("[a-c]", "g");
对于本篇文章的其余部分,将使用常量正则表达式来匹配测试字符串。
在 typescript 中使用 test
方法检查目标字符串中是否存在模式
regexp
的测试方法可用于通过返回 boolean
变量来检查目标字符串中是否存在特定模式或正则表达式。
const targetstring : string = "all is well";
// regex to check if 'all' word is present or not.
const rexp : regexp = /all/;
console.log(rexp.test(targetstring));
输出:
true
在 typescript 中使用 regexp
的字符串 match
或 exec
方法在目标字符串中查找匹配项
字符串中的 match
方法或 regexp
的 exec
方法可以找到目标字符串中与正则表达式对应的匹配项。
这两种方法在查找目标字符串中的第一个匹配项时行为相似。
const targetstring : string = "all is well";
const rexp : regexp = /all/;
console.log(rexp.exec(targetstring));
console.log(targetstring.match(rexp))
输出:
["all"]
["all"]
这些函数在查找匹配字符串的所有情况或使用全局标志集进行搜索方面有所不同。
const targetstring : string = "all is well and beautiful";
const rexp : regexp = /[a-c]/g;
console.log(rexp.exec(targetstring));
console.log(targetstring.match(rexp))
输出:
["a"]
["a", "b"]
但是,exec
函数也可以找到正则表达式对应的目标字符串中的匹配总数。
function getmatches( target : string, rexp : regexp, matches : array<regexpexecarray> = []) {
const matchifany = rexp.exec(target);
matchifany && matches.push(matchifany) && getmatches(target, rexp, matches);
return matches;
}
const rexp : regexp = /[a-c]/g
const test_string : string = "all is well and beautiful";
console.log(getmatches(test_string, rexp, []).map( arr => arr[0]));
输出:
["a", "b"]
在 typescript 中的 regexp
文字表达式中使用标志
正则表达式中的一些常见标志包括忽略案例标志 i
、全局标志 g
和多行标志 m
。
以下代码段演示了如何使用 i
和 m
标志。
const targetstring : string = `#1 first line
#2 second line
#3 third line`;
// regex to check word is present or not.
const rexp1 : regexp = /line/g;
const rexp2 : regexp = /line/gi;
const rexp3 : regexp = /^#\d/gi;
const rexp4 : regexp = /^#\d/gim;
console.log(targetstring.match(rexp1));
console.log(targetstring.match(rexp2));
console.log(targetstring.match(rexp3));
console.log(targetstring.match(rexp4));
输出:
["line"]
["line", "line", "line"]
["#1"]
["#1", "#2", "#3"]
因此,i
标志捕获了所有出现的 line
,无论大小写如何。
m
标志通常可用于在行的开头和结尾查找模式。
在这个例子中,设置了 m
标志,可以找到所有在行首以数字开头的匹配项。
转载请发邮件至 1244347461@qq.com 进行申请,经作者同意之后,转载请以链接形式注明出处
本文地址:
相关文章
在 typescript 中使用 try..catch..finally 处理异常
发布时间:2023/03/19 浏览次数:385 分类:
-
本文详细介绍了如何在 typescript 中使用 try..catch..finally 进行异常处理,并附有示例。
发布时间:2023/03/19 浏览次数:254 分类:
-
本教程指南通过特定的实现和编码示例深入了解了 typescript 中 declare 关键字的用途。
发布时间:2023/03/19 浏览次数:962 分类:
-
本篇文章演示了类的 get 和 set 属性以及如何在 typescript 中实现它。
在 typescript 中格式化日期和时间
发布时间:2023/03/19 浏览次数:269 分类:
-
本教程介绍内置对象 date() 并讨论在 typescript 中获取、设置和格式化日期和时间的各种方法。
在 typescript 中返回一个 promise
发布时间:2023/03/19 浏览次数:586 分类:
-
本教程讨论如何在 typescript 中返回正确的 promise。这将提供 typescript 中 returns promise 的完整编码示例,并完整演示每个步骤。
在 typescript 中定义函数回调的类型
发布时间:2023/03/19 浏览次数:1445 分类:
-
本教程说明了在 typescript 中为函数回调定义类型的ag捕鱼王app官网的解决方案。为了程序员的方便和方便,实施了不同的编码实践指南。
使用 npm 将 typescript 更新到最新版本
发布时间:2023/03/19 浏览次数:446 分类:
-
本教程说明了如何使用 npm 更新到最新版本的 typescript。这将为如何使用 npm 将 typescript 更新到最新版本提供完整的实际示例。