Notes on Day 17 of 30 Days of JavaScript

Table of Contents

Lessons Learned

Regex for multiple text

replace(/\b(?:this|that|there|those)\b/gi, '')
  • /.../: standard Regex syntax
  • \b: assert position at a word boundary; prevents partial match; exact match
  • (text1|text2): match with parameter 'this', 'that' and so on
  • |: or conditional; note that the text should be near this
  • g modifier: global match
  • i modifier: case-insensitive match
const text = 'This soup from that store was made over there with those vegetables'
(text.replace(/\b(?:this|that|there|those)\b/gi, '').trim()
> 'soup from  store was made over  with  vegetables'

References

Twitter, LinkedIn