Google Sheets: How to Replace Commas Inside or Outside Brackets

Published on

Replacing commas inside or outside brackets in Google Sheets can be tricky because there’s no built-in function to do this selectively. With a combination of REGEXREPLACE, SUBSTITUTE, and ArrayFormula, you can achieve it efficiently.

Why Replace Commas Inside or Outside Brackets?

Sometimes you have a long text string with tasks and sub-tasks. You may want to split main tasks by commas but keep sub-tasks grouped within brackets.

Example (cell A1):

Task 1, Task 2 (Sub Task 1, Sub Task 2), Task 3, Task 4, Task 5 (Sub Task 1, Sub Task 2, Sub Task 3), Task 6, Task 7

Expected Output (after replacing commas inside brackets):

Task 1
Task 2 (Sub Task 1| Sub Task 2)
Task 3
Task 4
Task 5 (Sub Task 1| Sub Task 2| Sub Task 3)
Task 6
Task 7

Notice that outer commas split the main tasks, while inner commas inside brackets are replaced with a pipe (|) to preserve grouping.

1. Replace Commas Inside Brackets

Use this formula in B1:

=ArrayFormula(
   LET(
      text, SPLIT(SUBSTITUTE(A1, ")", ")🐟"), "🐟"), 
      fnl, REGEXREPLACE(text, "\(([^)]*)\)", 
         "(" & SUBSTITUTE(REGEXEXTRACT(text,"\(([^)]*)\)"), ",", "|") & ")"), 
      JOIN(" ", IFNA(fnl, text))
   )
)

Output:

Task 1, Task 2 (Sub Task 1| Sub Task 2), Task 3, Task 4, Task 5 (Sub Task 1| Sub Task 2| Sub Task 3), Task 6, Task 7

Formula Breakdown

  • SUBSTITUTE(A1, ")", ")🐟") – Marks closing brackets to split the text.
  • SPLIT(..., "🐟") – Splits the text into chunks for each bracketed section.
    Google Sheets text split into separate chunks at closing brackets using SUBSTITUTE and SPLIT formula
  • REGEXREPLACE(..., "(" & SUBSTITUTE(REGEXEXTRACT(...), ",", "|") & ")")
    • Finds the first bracketed section ( … ) in each chunk.
    • REGEXEXTRACT captures the content inside the parentheses.
    • SUBSTITUTE(..., ",", "|") replaces commas inside the brackets with a pipe (|).
    • "(" & … & ")" wraps the modified content back in parentheses.
    • Because of the split, all bracketed sections in the original text are processed, one per chunk.
  • IFNA(fnl, text) – Keeps chunks without brackets unchanged.
  • JOIN(" ", …) – Combines all chunks back into a single string.

2. Replace Commas Outside Brackets

Use this formula in B1 if you want to replace commas outside brackets instead:

=ArrayFormula(
   LET(
      text, SPLIT(SUBSTITUTE(A1, ")", ")🐟"), "🐟"), 
      fnl, REGEXREPLACE(SUBSTITUTE(text, ",", "|"), "\(([^)]*)\)", 
         "(" & SUBSTITUTE(REGEXEXTRACT(text,"\(([^)]*)\)"), "|", ",") & ")"), 
      JOIN(" ", IFNA(fnl, SUBSTITUTE(text, ",", "|")))
   )
)

Output:

Task 1| Task 2 (Sub Task 1, Sub Task 2) | Task 3| Task 4| Task 5 (Sub Task 1, Sub Task 2, Sub Task 3) | Task 6| Task 7

How This Works (Difference from “Inside Brackets” Formula)

  • SUBSTITUTE(text, ",", "|") – Replaces all commas in the chunk with |.
  • REGEXREPLACE(..., "(" & SUBSTITUTE(REGEXEXTRACT(...), "|", ",") & ")")
    • Restores commas inside parentheses by replacing the temporary | back with ,.
  • JOIN(" ", IFNA(fnl, SUBSTITUTE(text, ",", "|"))) – Combines all chunks into a single string, ensuring chunks without parentheses still have outer commas replaced.

3. Split Tasks into Rows

After replacing commas, you can split your text into separate rows:

  1. Choose the delimiter based on your replacement:
    • "," if you replaced inside brackets.
    • "|" if you replaced outside brackets.
  2. Use SPLIT and TRIM to remove extra spaces:
=ArrayFormula(TRANSPOSE(TRIM(SPLIT(B1, "DELIMITER"))))

Replace "DELIMITER" with the appropriate character: , or |.

Result:

  • Each task or sub-task appears in its own row, neatly organized.
Prashanth KV
Prashanth KV
Your Trusted Google Sheets and Excel Expert Prashanth KV is a Diamond Product Expert in Google Sheets, officially recognized by Google for his contributions to the Docs Editors Help Community and featured in the Google Product Experts Directory. Explore his blog to learn advanced formulas, automation tips, and problem-solving techniques to elevate your spreadsheet skills.

Top Discussions

More like this

Pivot Table Formatting, Output & Special Behavior in Google Sheets

Pivot Tables in Google Sheets are powerful—but they can get tricky once you move...

Pivot Table Calculations & Advanced Metrics in Google Sheets

When it comes to built-in tools for data analysis and visualization in Google Sheets,...

Google Sheets Pivot Table Tutorial: Basics, Setup, and Date Grouping

The easiest way to summarize, analyze, and report data in Google Sheets is by...

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.