2017-04-10 54 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