Javascript 什么';这是打字稿;“类型”;match()函数的输入类型
从上面的文档中,我看到String.prototype.match()函数的输入是“regexp”。这显然不是一根绳子。它是什么类型的 在TypeScript中,如何声明输入变量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
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”);或者,您可以在不事先声明变量的情况下对其进行测试;您也可以选择在不事先声明变量的情况下对其进行测试。