Regular Expressions 101

Community Patterns

Community Library Entry

0

Regular Expression
PCRE2 (PHP >=7.3)

/
{[a-zA-Z]*\s*&{2}
/
gm

Description

We had an issue wherein people were attempting to use strings as booleans in JavaScript. E.G.:

return (
  <a>
    {thisIsAString && ...
  </a>
)

The assumption being that thisIsAString would always return a truthy/falsy value. However, when used in a JSX/TSX context, the string actually gets treated as a string, yielding "text must be in <Text/>" console errors and logic failures.

To solve this, these cases need to be handled with a double-bang (!!).

return (
  <a>
    {!!thisIsAString && ...
  </a>
)

To find all of these usages, I used this regex to search in WebStorm. I was then able to examine the usages and make changes as needed.

Submitted by Nedron - 3 years ago