How do I remove words from a string if they appear in an array?

Yup here’s the regular expression:

/\b(a|about|actually|almost|also|although|always|am|an|and|any|are|as|at|be|became|become|but|by|can|could|did|do|does|each|either|else|for|from|had|has|have|hence|how|i|if|in|is|it|its|just|may|maybe|me|might|mine|must|my|neither|nor|not|of|oh|ok|when|where|whereas|wherever|whenever|whether|which|while|who|whom|whoever|whose|why|will|with|within|without|would|yes|yet|you|your)\b/gi

Explanation:

  • / and / are the delimiters for the regular expression pattern.
  • \b is a word boundary that ensures the pattern matches whole words and not substrings within larger words.
  • (a|about|actually|almost|...|your) is an alternation group that matches any of the words in the list.
  • \b is another word boundary at the end of the pattern.
  • g is a flag that makes the search global, meaning it will replace all occurrences of the matched words.
  • i is a flag that makes the search case-insensitive.

you can put that as an argument to the replace() function.

something like this

{{replace("this Without about test"; "/\b(a|about|actually|almost|also|although|always|am|an|and|any|are|as|at|be|became|become|but|by|can|could|did|do|does|each|either|else|for|from|had|has|have|hence|how|i|if|in|is|it|its|just|may|maybe|me|might|mine|must|my|neither|nor|not|of|oh|ok|when|where|whereas|wherever|whenever|whether|which|while|who|whom|whoever|whose|why|will|with|within|without|would|yes|yet|you|your)\b/gi"; emptystring)}}

EDIT: looks like both the /g and /i flags CANNOT be used together in the replace() function. You can only use /g or /i but not /gi together. See below

6 Likes