Home > Blockchain >  Regular Expression
Regular Expression

Time:10-03

like make regular Expression,

buy_towars_${item.towars}_press // item.towars => (3821731.mol) or (m*ilk)

/^buy_towars_(\w )_press$/

I do this But it doesn't work

CodePudding user response:

If you want to match (3821731.mol) or (m*ilk) you have to escape the parenthesis to match them literally.

Note that \w does not match * or . so you can include that in a character class

^buy_towars_\([\w*.] \)_press$

Regex demo

Or a very broad way to match could be using a negated character class matching any character except parenthesis:

^buy_towars_\([^()]*\)_press$

Regex demo

const regex = /^buy_towars_\([\w*.] \)_press$/;

[{
    "towars": "(3821731.mol)"
  },
  {
    "towars": "(m*ilk)"
  }
].forEach(item =>
  console.log(regex.test(`buy_towars_${item.towars}_press`))
);

  • Related