简体   繁体   中英

Argument of type 'RegExp' is not assignable to parameter of type 'string'

I am trying to import an Excel File like so:

onFileChange(event: any) 
  {
    const inputFile: DataTransfer = <DataTransfer>(event.target); 
    const fileReader: FileReader = new FileReader();
    fileReader.onload = (event: any) => 
    {
      const binaryString: string = event.target.result;
      const workBook: XLSX.WorkBook = XLSX.read(binaryString, { type: 'binary', sheetStubs: true}); 
      /* sheetstubs true supposedly shows empty cells but isn't */
      console.log(typeof binaryString)

      const workSheetName: string = workBook.SheetNames[0];
      console.log(workSheetName)
      const workSheet: XLSX.WorkSheet = workBook.Sheets[workSheetName];

      this.data = <Array>(XLSX.utils.sheet_to_json(workSheet, 
      {header: 1, blankrows: true }));


    };
    fileReader.readAsBinaryString(inputFile.files[0]);

  }

and want to find the index (basically the column number) of any cell that contains the phrase 'cap' in it using regex.

I am trying to use regex like so below to search for the term cap in any of the cell but am met with the following error,

Argument of type 'RegExp' is not assignable to parameter of type 'string'. I am unclear as to what this error is trying to indicate to me and what might be a good way to achieve what I am trying to achieve.

getManufacturerDescriptionColumn()
  {
    for (const row in this.data)
    {
      var descIndex = row.indexOf(/cap*/i)
      return descIndex
    }
  }

The String.indexOf() method expects a string as it's first parameter.

The /<pattern>/ syntax is a regular expression literal and it results in the instantiation of a RegExp object. So, as the error message indicates, it is not a string .

To fix this, you should use a string instead of regular expression when you call String.indexOf() :

row.indexOf('cap')

This search is not case-insensitive though. If you want case insensitivity, then using a regular expression would be prudent.

To use a regular expression, what you want is the String.search() method . It expects a RegExp as it's first parameter and has an equivalent return value (ie the index within the string of where the first match begins).


A couple more observations:

  • The way your loop is written, it will return the result of the first iteration even if no match is found.

  • The regular expression you specified ( /cap*/i ) is looking for the characters ca followed by 0 or more p characters. So it will match ca , cap , capppp , etc.

Perhaps you want something like this:

getManufacturerDescriptionColumn() {
  for (const row in this.data) {
    let matchIndex = row.search(/cap/i);
    if (matchIndex > -1) {
      return matchIndex;
    }
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM