Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/463.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 什么';这是打字稿;“类型”;match()函数的输入类型_Javascript_Regex_String_Types_Typescript - Fatal编程技术网

Javascript 什么';这是打字稿;“类型”;match()函数的输入类型

Javascript 什么';这是打字稿;“类型”;match()函数的输入类型,javascript,regex,string,types,typescript,Javascript,Regex,String,Types,Typescript,从上面的文档中,我看到String.prototype.match()函数的输入是“regexp”。这显然不是一根绳子。它是什么类型的 在TypeScript中,如何声明输入变量 regex:regexp = ^\d{2}\/\d{2}\/\d{4}$ 上面的代码显然抛出了一个错误,因为regexp不是可识别的类型。我如何修复它?试试这个 //inside the class //this expression is to test valid email public reg: RegEx

从上面的文档中,我看到String.prototype.match()函数的输入是“regexp”。这显然不是一根绳子。它是什么类型的

在TypeScript中,如何声明输入变量

regex:regexp = ^\d{2}\/\d{2}\/\d{4}$
上面的代码显然抛出了一个错误,因为regexp不是可识别的类型。我如何修复它?

试试这个

//inside the class
//this expression is to test valid email

public reg: RegExp = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;
然后你可以在课堂上测试它

this.reg.test("expression to test")
//课外

let reg = /^\d+$/;
alert(reg.test("sd")); //will alert false
试试这个

//inside the class
//this expression is to test valid email

public reg: RegExp = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;
然后你可以在课堂上测试它

this.reg.test("expression to test")
//课外

let reg = /^\d+$/;
alert(reg.test("sd")); //will alert false

您可以在
lib.d.ts
中查看类型信息:

/**
  * Matches a string with a regular expression, and returns an array containing the results of that search.
  * @param regexp A variable name or string literal containing the regular expression pattern and flags.
  */
match(regexp: string): RegExpMatchArray;

/**
  * Matches a string with a regular expression, and returns an array containing the results of that search.
  * @param regexp A regular expression object that contains the regular expression pattern and applicable flags.
  */
match(regexp: RegExp): RegExpMatchArray;

您可以看到正则表达式的类型是
RegExp
,而
match
有两个定义,一个是字符串,另一个是RegExp。

您可以在
lib.d.ts
中查看类型信息:

/**
  * Matches a string with a regular expression, and returns an array containing the results of that search.
  * @param regexp A variable name or string literal containing the regular expression pattern and flags.
  */
match(regexp: string): RegExpMatchArray;

/**
  * Matches a string with a regular expression, and returns an array containing the results of that search.
  * @param regexp A regular expression object that contains the regular expression pattern and applicable flags.
  */
match(regexp: RegExp): RegExpMatchArray;

您可以看到正则表达式的类型是
RegExp
,而
match
有两个定义,一个是字符串,另一个是RegExp。

var regex=/^\d{2}\/\d{2}\/\d{4}$/g regex.test(您的变量)//这将输出true和falseresult@Deep它的类型定义是什么?我如何在Typescript中声明它?类似于regex:regexp=…,参数可以是一个或
字符串
。另外,请注意,这些参数用斜杠分隔(类似于字符串文本的引号)–
/^\d{2}\/\d{2}\/\d{4}$/
。您不需要指定类型,因为该值将提供该类型(只要将其括在斜杠中)。仅供参考,要匹配的参数类型将是
RegExp|string
.var regex=/^\d{2}\/\d{2}\/\d{4}$/g regex.test(您的变量)//这将输出true和falseresult@Deep它的类型定义是什么?我如何在Typescript中声明它?类似于regex:regexp=…,参数可以是一个或
字符串
。另外,请注意,这些参数用斜杠分隔(类似于字符串文本的引号)–
/^\d{2}\/\d{2}\/\d{4}$/
。您不需要指定类型,因为该值将提供该类型(只要将其括在斜杠中)。仅供参考,要匹配的参数类型将是
RegExp|string
.alert(/^\d+$/.test(“sd”);或者,您可以在不事先声明变量的情况下对其进行测试;您也可以选择在不事先声明变量的情况下对其进行测试。