Javascript String Match function
从上面的文档中我看到 String.prototype.match() 函数的输入是“regexp”。这显然不是一个字符串。它的类型是什么?
在 TypeScript 中如何声明输入变量?
regex:regexp = ^\d{2}\/\d{2}\/\d{4}$
上面显然会抛出错误,因为 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。
关于javascript - match() 函数输入的 TypeScript "type"是什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38464168/