javascript – TypeScript和RegExp
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了javascript – TypeScript和RegExp,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2231字,纯文字阅读大概需要4分钟。
内容图文
TypeScript说:
The property ‘
$1
‘ does not exist on value of type ‘{ (pattern: string, flags?: string): RegExp; new(pattern: string, flags?: string): RegExp; }
‘
通过查看TypeScript 0.8.2附带的lib.d.ts中的定义可以解释该类型:
interface RegExp {
/**
* Executes a search on a string using a regular expression pattern, and returns an array containing the results of that search.
* @param string The String object or string literal on which to perform the search.
*/
exec(string: string): RegExpExecArray;
/**
* Returns a Boolean value that indicates whether or not a pattern exists in a searched string.
* @param string String on which to perform the search.
*/
test(string: string): bool;
/** Returns a copy of the text of the regular expression pattern. Read-only. The rgExp argument is a Regular expression object. It can be a variable name or a literal. */
source: string;
/** Returns a Boolean value indicating the state of the global flag (g) used with a regular expression. Default is false. Read-only. */
global: bool;
/** Returns a Boolean value indicating the state of the ignoreCase flag (i) used with a regular expression. Default is false. Read-only. */
ignoreCase: bool;
/** Returns a Boolean value indicating the state of the multiline flag (m) used with a regular expression. Default is false. Read-only. */
multiline: bool;
lastIndex: number;
}
declare var RegExp: {
new (pattern: string, flags?: string): RegExp;
(pattern: string, flags?: string): RegExp;
}
我的问题是如何更改/更新这个以允许我引用RegExp.$1,RegExp.$2等?最好我可以单独声明,因为我不打算直接编辑lib.d.ts(很可能会在更新时更换)
我试过这个无济于事:
declare var RegExp: {
new (pattern: string, flags?: string): RegExp;
(pattern: string, flags?: string): RegExp;
$1: any;
$2: any;
$3: any;
$4: any;
$5: any;
$6: any;
$7: any;
$8: any;
$9: any;
}
我猜他们应该用字符串实际声明.但无论如何它都不会删除错误.
除此之外,我也很好奇这究竟是什么意思(为什么声明它有没有新的?):
new (pattern: string, flags?: string): RegExp;
(pattern: string, flags?: string): RegExp;
解决方法:
你的声明var很接近.接口是开放的,所以你可以写:
interface RegExp {
$1: string;
$2: string;
// etc
}
关于第二个,这是因为新的RegExp(‘[A-Z]’)和RegExp(‘[A-Z]’)都可以创建一个RegExp(在类型方面,它既是一个可调用函数又是一个构造函数).
内容总结
以上是互联网集市为您收集整理的javascript – TypeScript和RegExp全部内容,希望文章能够帮你解决javascript – TypeScript和RegExp所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。