From 33058f2b292b3a581333bdfb21b8f671898c5060 Mon Sep 17 00:00:00 2001 From: Peter Bengtsson Date: Tue, 8 Dec 2020 14:40:17 -0500 Subject: initial commit --- .../global_objects/regexp/test/index.html | 128 +++++++++++++++++++++ 1 file changed, 128 insertions(+) create mode 100644 files/zh-cn/web/javascript/reference/global_objects/regexp/test/index.html (limited to 'files/zh-cn/web/javascript/reference/global_objects/regexp/test') diff --git a/files/zh-cn/web/javascript/reference/global_objects/regexp/test/index.html b/files/zh-cn/web/javascript/reference/global_objects/regexp/test/index.html new file mode 100644 index 0000000000..d742f6c172 --- /dev/null +++ b/files/zh-cn/web/javascript/reference/global_objects/regexp/test/index.html @@ -0,0 +1,128 @@ +--- +title: RegExp.prototype.test() +slug: Web/JavaScript/Reference/Global_Objects/RegExp/test +tags: + - JavaScript + - Method + - Prototype + - RegExp + - Regular Expressions +translation_of: Web/JavaScript/Reference/Global_Objects/RegExp/test +--- +
{{JSRef}}
+ +

test() 方法执行一个检索,用来查看正则表达式与指定的字符串是否匹配。返回 truefalse

+ +
{{EmbedInteractiveExample("pages/js/regexp-prototype-test.html", "taller")}}
+ + + +

语法

+ +
regexObj.test(str)
+ +

参数

+ +
+
str
+
用来与正则表达式匹配的字符串
+
+ +

返回值

+ +

如果正则表达式与指定的字符串匹配 ,返回true;否则false

+ +

描述

+ +

当你想要知道一个正则表达式是否与指定的字符串匹配时,就可以使用 test()(类似于 {{jsxref("String.prototype.search()")}} 方法),差别在于test返回一个布尔值,而 search 返回索引(如果找到)或者-1(如果没找到);若想知道更多信息(然而执行比较慢),可使用{{jsxref("RegExp.prototype.exec()", "exec()")}} 方法(类似于 {{jsxref("String.prototype.match()")}} 方法)。 和 {{jsxref("RegExp.prototype.exec()", "exec()")}} (或者组合使用),一样,在相同的全局正则表达式实例上多次调用test将会越过之前的匹配。

+ +

示例

+ +

使用 test()

+ +

一个简单的例子,测试 "hello" 是否包含在字符串的最开始,返回布尔值。

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

下例打印一条信息,该信息内容取决于是否成功通过指定测试:

+ +
function testinput(re, str){
+    var midstring;
+    if (re.test(str)) {
+        midstring = " contains ";
+    } else {
+        midstring = " does not contain ";
+    }
+    console.log(str + midstring + re.source);
+}
+
+ +

当设置全局标志的正则使用test()

+ +

如果正则表达式设置了全局标志,test() 的执行会改变正则表达式   lastIndex属性。连续的执行test()方法,后续的执行将会从 lastIndex 处开始匹配字符串,({{jsxref("RegExp.prototype.exec()", "exec()")}} 同样改变正则本身的 lastIndex属性值).

+ +

下面的实例表现了这种行为: 

+ +
var regex = /foo/g;
+
+// regex.lastIndex is at 0
+regex.test('foo'); // true
+
+// regex.lastIndex is now at 3
+regex.test('foo'); // false
+
+ +

规范

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
SpecificationStatusComment
{{SpecName('ES3')}}{{Spec2('ES3')}}Initial definition. Implemented in JavaScript 1.2.
{{SpecName('ES5.1', '#sec-15.10.6.3', 'RegExp.test')}}{{Spec2('ES5.1')}}
{{SpecName('ES6', '#sec-regexp.prototype.test', 'RegExp.test')}}{{Spec2('ES6')}}
{{SpecName('ESDraft', '#sec-regexp.prototype.test', 'RegExp.test')}}{{Spec2('ESDraft')}}
+ +

+ +

浏览器兼容性

+ + + +

{{Compat("javascript.builtins.RegExp.test")}}

+ +

Firefox特殊注意

+ +

在 Firefox 8之前, test() 被不正确地实现了;当无参数地调用时,它会匹配之前的输入值 (RegExp.input 属性),而不是字符串"undefined"。这已经被修正了;现在 /undefined/.test() 正确地返回true,而不是错误。

+ +

相关链接

+ + -- cgit v1.2.3-54-g00ecf