RegExp.prototype.test()
RegExp
实例的 test()
方法使用此正则表达式执行搜索,以查找正则表达式与指定字符串之间的匹配项。如果匹配则返回 true
;否则为 false
。
¥The test()
method of RegExp
instances executes a search with this regular expression for a match between a regular expression and a specified string. Returns true
if there is a match; false
otherwise.
JavaScript RegExp
对象在设置了 global
或 sticky
标志(例如 /foo/g
或 /foo/y
)时是有状态的。他们存储了上一场比赛的 lastIndex
。在内部使用此功能,test()
可用于迭代文本字符串(带有捕获组)中的多个匹配项。
¥JavaScript RegExp
objects are stateful when they have
the global
or sticky
flags
set (e.g., /foo/g
or /foo/y
). They store a
lastIndex
from the previous match. Using this
internally, test()
can be used to iterate over multiple matches in a string
of text (with capture groups).
Try it
语法
参数
返回值
描述
¥Description
每当你想知道是否在字符串中找到模式时,请使用 test()
。test()
返回一个布尔值,与 String.prototype.search()
方法不同(它返回匹配项的索引,如果未找到则返回 -1
)。
¥Use test()
whenever you want to know whether a pattern is found in a
string. test()
returns a boolean, unlike the
String.prototype.search()
method (which returns the index of a match, or
-1
if not found).
要获取更多信息(但执行速度较慢),请使用 exec()
方法。(这与 String.prototype.match()
方法类似。)
¥To get more information (but with slower execution), use the
exec()
method. (This is similar to the
String.prototype.match()
method.)
与 exec()
(或与其组合)一样,在同一个全局正则表达式实例上多次调用 test()
将超越前一个匹配项。
¥As with exec()
(or in combination with it), test()
called
multiple times on the same global regular expression instance will advance past the
previous match.
示例
使用测试()
¥Using test()
测试 "hello"
是否包含在字符串开头的简单示例,返回布尔结果。
¥Simple example that tests if "hello"
is contained at the very beginning of
a string, returning a boolean result.
const str = "hello world!";
const result = /^hello/.test(str);
console.log(result); // true
以下示例记录一条取决于测试是否成功的消息:
¥The following example logs a message which depends on the success of the test:
function testInput(re, str) {
const midstring = re.test(str) ? "contains" : "does not contain";
console.log(`${str} ${midstring} ${re.source}`);
}
在带有 "global" 标志的正则表达式上使用 test()
¥Using test() on a regex with the "global" flag
当正则表达式设置了 全局标志 时,test()
将推进正则表达式的 lastIndex
。(RegExp.prototype.exec()
还推进了 lastIndex
属性。)
¥When a regex has the global flag set,
test()
will advance the lastIndex
of the regex.
(RegExp.prototype.exec()
also advances the lastIndex
property.)
进一步调用 test(str)
将从 lastIndex
开始继续搜索 str
。每次 test()
返回 true
时,lastIndex
属性都会继续增加。
¥Further calls to test(str)
will resume searching
str
starting from lastIndex
. The
lastIndex
property will continue to increase each time test()
returns true
.
注意:只要
test()
返回true
,lastIndex
就不会重置 - 即使测试不同的字符串时也是如此!¥Note: As long as
test()
returnstrue
,lastIndex
will not reset—even when testing a different string!
当 test()
返回 false
时,调用正则表达式的 lastIndex
属性将重置为 0
。
¥When test()
returns false
, the calling regex's
lastIndex
property will reset to 0
.
以下示例演示了此行为:
¥The following example demonstrates this behavior:
const regex = /foo/g; // the "global" flag is set
// regex.lastIndex is at 0
regex.test("foo"); // true
// regex.lastIndex is now at 3
regex.test("foo"); // false
// regex.lastIndex is at 0
regex.test("barfoo"); // true
// regex.lastIndex is at 6
regex.test("foobar"); // false
// regex.lastIndex is at 0
regex.test("foobarfoo"); // true
// regex.lastIndex is at 3
regex.test("foobarfoo"); // true
// regex.lastIndex is at 9
regex.test("foobarfoo"); // false
// regex.lastIndex is at 0
// (...and so on)
规范
Specification |
---|
ECMAScript Language Specification # sec-regexp.prototype.test |
浏览器兼容性
BCD tables only load in the browser