JavaScript中RegExp的test方法详解

本文详细介绍了正则表达式RegExp.prototype.test()方法的使用,包括其语法、参数、返回值及如何在字符串中查找模式。通过实例演示了在不同情况下test()方法的行为,特别是当使用g修饰符时lastIndex的影响。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

RegExp.prototype.test()

** RegExp.prototype.test()方法为指定正则表达式和指定字符串执行一次匹配,返回true或false。**

var regex1 = RegExp('foo*');
var regex2 = RegExp('foo*','g');
var str1 = 'table football';

console.log(regex1.test(str1));
// 输出: true

console.log(regex1.test(str1));
// 输出: true

console.log(regex2.test(str1));
// 输出: true

console.log(regex2.test(str1));
// 因为g修饰符的存在,lastIndex改变,输出: false

语法

regexpObj.test(str)

参数

str
需要匹配模式的字符串。

返回值

如果匹配成功,返回true。否则返回false。

描述

如果想知道一个字符串中是否存在一个模式,调用test()函数即可。不像String.prototype.search()返回匹配项的索引,RegExp.prototype.test()函数只返回一个布尔值。如果想要获取更多信息,调用RegExp.prototype.exec()函数或String.prototype.match()函数,但是这两个函数的执行效率会低于RegExp.prototype.test()函数。RegExp.prototype.exec()和RegExp.prototype.test一样,在设置了g修饰符之后,每次执行都会设置lastIndex,从而影响下一次匹配的结果。

示例

使用RegExp.prototype.test()

下面代码查找一个字符串中是否以”hello”开头:

var str = 'hello world!';
var result = /^hello/.test(str);
console.log(result); // true

在一个包含g修饰符的正则上使用RegExp.prototype.test()函数

如果正则表达式被设置了g修饰符,RegExp.prototype.test()函数会在每一次调用后设置正则表达式的lastIndex值。在其后调用的test()会从这个lastIndex索引位置开始匹配(RegExp.prototype.exec()函数也遵循同样的规则)。如果前后传入不同的字符串作为参数,RegExp.prototype.test()的结果就是没有意义的。
下面的代码展示了这些特性:

var regex = /foo/g;

// regex.lastIndex = 0
regex.test('foo'); // true

// regex.lastIndex = 3
regex.test('foo'); // false

// regex.lastIndex = 0
regex.test('barfoo') // true

// regex.lastIndex = 6
regex.test('foobar') //false

利用这种机制,下面的代码建立了一个函数,用来查找在一个字符串中一共有多少单词:

function countWords (sText) {

  for (var rWord = /\w+/g, nCount = 0; rWord.test(sText); nCount++);

  return nCount;

}

console.log(countWords("What a beautiful day!")); // 4
Regex Testor Version 1.02 Copyright (c) 2013 Fiery Red - flameleo 我们在使用正则表达式( regex: regular expression )的过程中,经常发现正则表达式的语法很令人头疼,即使对经常使用它的人来说也是如此。对于刚接触正则表达式的人来说多练习,多使用,才能熟练掌握正则表达式。 由于难于读写,容易出错,且需要反复练习。所以找一种工具对正则表达式对我们脑中构思的regex进行测试是很有必要的。 1.特点 a.适合初学者,在不断测试用学习如何使用正则表达式。 b.可以分组保存,测试中用到的正则表达式。 c.本工具使用最常见的regex。以下是简单示例: a|b Matches a or b gr(a|e)y Matches a or e . Matches any single character [abc] Matches a single character a, b or c [^abc] Matches any single character except a, b or c [a-z] Matches a single charactor in the range a to z [a-zA-Z] Matches a single charactor in the range a to z or A to Z ^ Matches the start of the filename $ Matches the end of the filename * Matches the preceding element zero or more times ? Matches the preceding element zero or one times + Matches the preceding element one or more times {x} Matches the preceding element x times {x,} Matches the preceding element x or more times {x,y} Matches the preceding element between x and y times 值得一提的是()代表子匹配,有些环境中gr(a|e)y Matches gray or grey 还支持许多常见的转义字符 \b,\B,\c,\d,\D,\f,\n,\r,\s,\S,\t,\v,\w,\W,\x,\u 具体详见附件 Regular Expression Syntax1.html 2.功能介绍 a.界面上显示提供regex输入框和原文本框,点击[模式匹配]按钮后,会在右侧输出结果,包括匹配字符串列表和文本。 b.对于测试中一些有用的regex,点击[insert]按钮添加到模式列表,以备日后使用。你可以位该regex添加描述分组,该信息会在程序结束后保存在CustomPatternInfo.ini文件中。 c.可以参考Readme_1.jpq和Readme_2.jpq图片介绍。 3.有待改进 a.界面布局和控件友好型和交互性。 b.界面功能提供regex语法支持。(暂时可以通过导入附件RegexSystax.ini到CustomPatternInfo.ini中) 4.意见反馈 a.请将您的宝贵意见反馈到 FieryRed_2012@163.com 附件: Readme.txt Readme_1.jpg Readme_2.jpg Regular Expression Syntax.html RegexSystax.ini
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值