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

  • Home
  • SEARCH
  • 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 8671837
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T19:03:11+00:00 2026-06-12T19:03:11+00:00

I need to use Java to replace some JavaScript in a file. The steps

  • 0

I need to use Java to replace some JavaScript in a file. The steps I’m taking are basically like this:

  1. Read in a file (it’s an .asp file with JavaScript in it that is generated from another system)
  2. Replace the unwanted JavaScript function with one I do want
  3. Write it back out over the file that was originally generated

I’m having trouble with step 2 because my code seems to freak out on the replaceAll method. Let’s say I want to replace this method:

function sayHi() {
    alert('hi');
}

…with something like this:

function sayHi() {
    alert("Hello, World!");
}

I have code that looks like this:

private static final String REPLACEMENT_METHOD =
        "function sayHi() {\n" +
        "   alert('Hello, World!');\n" +
        "}";

private static final String METHOD_TO_REPLACE =
        "function sayHi() {\n" +
        "   alert('hi');\n" +
        "}";

String content = // Get the contents from the file...

content = content.replaceAll(METHOD_TO_REPLACE, REPLACEMENT_METHOD);

The problem I’m running into is that the string that I want to replace is being interpreted as a regex (it’s just literal text, but it is, of course, interpreted as a regex) and it bails out on me. Unfortunately, I’ve not been able to find the correct syntax to escape the special characters properly and still get it to match the way I want.

Any ideas?

Thanks!

  • 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-06-12T19:03:12+00:00Added an answer on June 12, 2026 at 7:03 pm

    You can use quote operators around your match pattern:

    private static final String METHOD_TO_REPLACE =
        "\\Q" +
        "function sayHi() {\n" +
        "   alert('hi');\n" +
        "}" +
        "\\E";
    

    By the way,

    function sayHi() {
        alert("Hello, World!");
    }
    

    isn’t going to work in Java. You probably want:

    void sayHi() {
        alert("Hello, World!");
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need a regex to use in Java to replace a String with UNKNOWN
I am developing an asp.net web application. I need to use java script to
I need to import some Excel spreadsheets into Java objects. I will use POI
I am converting Java code to C# and need to replace the use of
I need to replace some string from a text file such as upcoming:event=123982. The
Is there a java equivalent of .NET's System.Xml.XmlNode.InnerXml ? I need to replace some
I need to use a replace function in Java: string.replace(myString,); myString values are for
I am developping an Android app, and I need to use java Signature class
I need a shallow copy of an java ArrayList , should I use clone()
I need to invoke a mapreduce job from java application. I use ToolRunner.run(new Validation(),

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.