简体   繁体   中英

Regex - match word or emoji from list - nodejs

I'm working on a bot that has a match terms function. It's given text and terms list that it can match with (words/emojis). Here's my code that mostly works:

function matchTerms (terms, text) {
  //   > /(^|[^\w])🚧/i.test('🚧')
  //   < true
  //   > /(^|[^\w])🚧/i.test('foo🚧')
  //   < false
  //   > /(^|[^\w])🚧/i.test('foo 🚧')
  //   < true
  const matches = text.match(new RegExp(`(^|[^\\w])(${terms.join('|')})([^\\w]|$)`, 'i'))
  return matches ? matches[2] : null
}

This works. But my issue is if my matching term is an emoji and that emoji is attached to another word this regex won't find it. Anyone know a possible solution?

这样简单吗?

 console.log(/🚧/i.test('foo🚧')) 

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