2017-04-10 63 views
1

我想使用打字稿中的RegExp,但是在Visual Studio中代码被标记为错误。typescript import {RegExp}

  import { RegExp } from "RegularExpressions/Regex"; // module not found. 
      export class BotAnswerRegex { 
       createRegex(){ 
        var regex = new RegExp('hi'); 
        // regex.isMatch is marked as an error 
        var result = regex.isMatch('hi, i am peter'); 
       } 
      } 

我正在关注文档。 http://electricessence.github.io/TypeScript.NET/documentation/classes/_source_system_text_regularexpressions_.regex.html

更新: 修正变量声明

回答

2

要使用此libarary你的代码应该是这样的:

import RegExp from "typescript-dotnet-commonjs/System/Text/RegularExpressions"; 
// ... 
let regex = new RegExp('hi'); 
let result = regex.isMatch('hi, i am peter'); 
// ... 
console.log(result); 
// ... 

UPDATE

请注意,你的代码是不正确的,你宣布本地变种:

var regex = new RegExp('hi'); 

,且您尝试访问使用this此变种:

... this.regex.isMatch('hi, i am peter'); 
+0

仍然是错误'[TS]找不到模块 '打字稿,DOTNET-CommonJS的/系统/文字/ RegularExpressions'。 ' – roll

+0

如何安装'typescript-dotnet-commonjs'模块?尝试使用'npm install typescript-dotnet-commonjs' – Diullei

+0

LOL,我的错。作品!!! – roll