55

I want to apply Reg Expression on string. In order to get all groups result i am using matchAll method. Here is my code

const regexp = RegExp('foo*','g'); 
const str = "table football, foosball";
let matches = str.matchAll(regexp);

for (const match of matches) {
   console.log(match);
}

during compiling on above code i got error

Property 'matchAll' does not exist on type '"table football, foosball"'

during searching about this error i found similar issue on stackoverflow

TS2339: Property 'includes' does not exist on type 'string'

I changed tsconfig configuration as mention in above link but my issue did not solved

Here is my tsconfig code;

{
 "compileOnSave": false,
 "compilerOptions": {
 "baseUrl": "./",
 "importHelpers": true,
 "outDir": "./dist/out-tsc",
 "sourceMap": true,
 "declaration": false,
 "module": "es2015",
 "moduleResolution": "node",
 "emitDecoratorMetadata": true,
 "experimentalDecorators": true,
 "target": "es2016",
 "typeRoots": [
  "node_modules/@types"
 ],
"lib": [
  "es2018",
  "dom"
]
}
}
Yousuf
  • 760
  • 1
  • 5
  • 13

3 Answers3

78

String.prototype.matchAll() is part of the ECMAScript 2020 specification (draft). In TypeScript you can include these library features by adding es2020 or es2020.string, in the compiler options:

"compilerOptions": {
    "lib": ["es2020.string"]
}
mvr
  • 1,732
  • 16
  • 11
  • 1
    i have just added this as i was having the same issues as OP. But i now get the following error in my cli console. .matchAll is not a function. Any idea on what might be wrong? – Andy Oct 29 '19 at 12:49
  • 2
    If you're getting `matchAll is not a function` as a runtime error this might be because your using typescript as a typechecker and using something else (eg webpack + babel) as a transpiler. You'll therefore still need to find a way to polyfil matchAll function – Jay Wick Feb 13 '20 at 22:24
  • 3
    I got some very weird behaviour with `"lib": ["es2020.string"]` in jsconfig.json. Using `"target": "es2020"` worked much better. Also had to change node version in launch.json, for some reason node didn't have matchAll even though vsc claimed it was running v15, no idea how that happened. – Tamir Daniely Apr 23 '21 at 20:36
  • changing target to es2020 solved it for me. thanks – ufk May 18 '21 at 15:49
3

Checking the ts signature for the String and RegExp classes I realized there is no signature for matchAll. One manner to solve it could be:

let matches = str['matchAll'](regexp);

Another way would be adding the method to lib.es5.d.ts file enter image description here

Sergio Escudero
  • 1,798
  • 14
  • 21
  • so how this code works here https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/match – Yousuf Apr 03 '19 at 16:22
  • thnks @SergioEscudero i checked your first suggestion and its working great as expected but strangely its working on chrome browser but on Firefox it through error "str.matchAll is not a function". i will check your second suggestion. – Yousuf Apr 03 '19 at 18:07
  • Check the browser compatibility https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/@@matchAll#Browser_compatibility – Sergio Escudero Apr 03 '19 at 18:23
  • The work around str['matchAll'](regexp) gives error '...because index expression is not of type 'number'. if Str is a string doesn't str[x] expect a numeric index? – GGizmos Jan 15 '20 at 07:40
1

In my case I used outdated version of the Node shipped by apt package manager (v10.19.0). After I upgraded to the latest (v16.11.1) issue had gone.

Following is summary of commands I used:

sudo npm cache clean -f
sudo npm install -g n
sudo n stable

sudo n latest

The source: https://askubuntu.com/questions/426750/how-can-i-update-my-nodejs-to-the-latest-version