Regular Expressions 101

Community Patterns

Community Library Entry

1

Regular Expression
ECMAScript (JavaScript)

/
(\w+\s*\n*=\s*\n*)\{\n*\s*`((?:[^`](?!(?:\$\{|\n|(?:'[^`]*"|"[^`]*'))))*)`\n*\s*\}
/
gm

Description

Matches string properties that unnecessarily use backticks in React components.

NOTE: This regex ignoes backtick strings that contain BOTH quotes (") AND single quotes (') but it WILL match backtick strings with ONLY quotes or ONLY single quotes! Keep this in mind if you want to use substitution!

For example, if the string is: prop={`String with ONLY "quotes" and NO single quotes`}

Then the regex will match these capture groups: $1 -> prop= $2 -> String with ONLY "quotes" and NO single quotes

So if you use substitution with this input: $1"$2"

You will get: prop="String with ONLY "quotes" and NO single quotes"

Which will give you an error!!!

Submitted by Nathanthanthan - 6 days ago (Last modified 6 days ago)