Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 929491
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T20:10:51+00:00 2026-05-15T20:10:51+00:00

I want to apply a certain regular expression substitution globally to about 40 Javascript

  • 0

I want to apply a certain regular expression substitution globally to about 40 Javascript files in and under a directory. I’m a vim user, but doing this by hand can be tedious and error-prone, so I’d like to automate it with a script.

I tried sed, but handling more than one line at a time is awkward, especially if there is no limit to how many lines the pattern might match.

I also tried this script (on a single file, for testing):

ex $1 <<EOF
gs/,\(\_\s*[\]})]\)/\1/
EOF

The pattern will eliminate a trailing comma in any Perl/Ruby-style list, so that “[a, b, c,]” will come out as “[a, b, c]” in order to satisfy Internet Explorer, which alone among browsers, chokes on such lists.

The pattern works beautifully in vim but does nothing if I run it in ex, as per the above script.

Can anyone see what I might be missing?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T20:10:52+00:00Added an answer on May 15, 2026 at 8:10 pm

    You asked for a script, but you mentioned that you are vim user. I tend to do project-wide find and replace inside of vim, like so:

    :args **/*.js | argdo %s/,\(\_\s*[\]})]\)/\1/ge | update
    

    This is very similar to the :bufdo solution mentioned by another commenter, but it will use your args list rather than your buflist (and thus doesn’t require a brand new vim session nor for you to be careful about closing buffers you don’t want touched).

    • :args **/*.js – sets your arglist to contain all .js files in this directory and subdirectories
    • | – pipe is vim’s command separator, letting us have multiple commands on one line
    • :argdo – run the following command(s) on all arguments. it will “swallow” subsequent pipes
      • % – a range representing the whole file
      • :s – substitute command, which you already know about
      • :s_flags, ge – global (substitute as many times per line as possible) and suppress errors (i.e. “No match”)
      • | – this pipe is “swallowed” by the :argdo, so the following command also operates once per argument
      • :update – like :write but only when the buffer has been modified

    This pattern will obviously work for any vim command which you want to run on multiple files, so it’s a handy one to keep in mind. For example, I like to use it to remove trailing whitespace (%s/\s\+$//), set uniform line-endings (set ff=unix) or file encoding (set filencoding=utf8), and retab my files.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want a certain Javascript function to be called when the user mouse-overs /
I want to apply a attribute to all input[type=text] elements but exclude certain elements.
I want apply margin property for my html newsletter But Gmail ignores this CSS
I want to apply texture to the transparent png(bitmap), but I don't want to
I want to apply scaling animation to UIButton.When user drag button upside of the
I want to be able to apply certain rules to the products added to
I want to apply a transformation to certain columns of a record array and
I want to apply the rules in a CSS file to a certain div/class
I'm customising a tumblr blog, and I want to apply certain styles and rules
i want to apply masking over the textinput that need to get SSN from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.