DEV Community

zak100
zak100

Posted on

Javascript: Searching a String containing parenthesis

Hi,
I am using a string containing brackets as an argument to search. I am getting following error:
Using network 'development'.

`

SyntaxError: Invalid regular expression: /send(/: Unterminated group
at String.search ()
at module.exports (/home/zulfi/Truffle_programs/js_search_opcode_js/search_sendb.js:15:22)
at Object.exec (/home/zulfi/.nvm/versions/node/v10.23.3/lib/node_modules/truffle/build/webpack:/packages/require/require.js:127:1)
at Promise (internal/util.js:274:30)
at new Promise ()
at bound exec (internal/util.js:273:12)
at Object.run (/home/zulfi/.nvm/versions/node/v10.23.3/lib/node_modules/truffle/build/webpack:/packages/core/lib/commands/exec.js:80:1)
at process._tickCallback (internal/process/next_tick.js:68:7)
`

My code is:

`var assert = require('assert');
const path = require("path");
const fs = require("fs");

module.exports = async function(callback)
{
try{
   let keyWordStr1 = "address1.send(400)"
   let keyWordStr2 = "address2.transfer(500)"
   let commandS="send("
   let commandT="transfer("
   let sendStr = ""
   let transferStr = ""

   if(keyWordStr1.search(commandS) >= 0){
      sendStr = keyWordStr1
      console.log(sendStr)
   }
   if(keyWordStr2.search(commandT) >= 0){
      transferStr = keyWordStr[k]
      console.log(transferStr)
   }
}//try
catch (error) {
console.log(error)
}
callback();


}
`
Enter fullscreen mode Exit fullscreen mode

Somebody please guide me.

Zulfi.

Top comments (2)

Collapse
 
caxco93 profile image
caxco93

The search method is expecting a RegExp and you are passing a string that is not a valid RegExp.
You have 2 options:

A. Change commandS and commandT to a valid RegExp so the search method receives the correct argument type (making sure to escape the ( with a \):

   let commandS=/send\(/;
   let commandT=/transfer\(/;
Enter fullscreen mode Exit fullscreen mode

or

B. Use the indexOf method instead of search:

   if(keyWordStr1.indexOf(commandS) >= 0){
Enter fullscreen mode Exit fullscreen mode
Collapse
 
zak100 profile image
zak100

Hi,
Thanks.
God blesses you.

Zulfi.