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 8969139
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T17:33:03+00:00 2026-06-15T17:33:03+00:00

I am using Java to parse through a JavaScript file. Because the scope is

  • 0

I am using Java to parse through a JavaScript file. Because the scope is different than expected in the environment in which I am using it, I am trying to replace every instance of i.e.

test = value

with

window.test = value

Previously, I had just been using

writer.append(js.getSource().replaceAll("test", "window.test"));

which obviously isn’t generalizable, but for a fixed dataset it was working fine.

However, in the new files I’m supposed to work with, an updated version of the old ones, I now have to deal with

window['test'] = value

and

([[test]])

I don’t want to match test in either of those cases, and it seems like those are the only two cases where there’s a new format. So my plan was to now do a regex to match anything except ' and [ as the first character. That would be ([^'\[])test; however, I don’t actually want to replace the first character – just make sure it’s not one of the two I don’t want to match.

This was a new situation for me because I haven’t worked with replacement with RegExps that much, just pattern matching. So I looked around and found what I thought was the solution, something called “non-capturing groups”. The explanation on the Oracle page sounded like what I was looking for, but when I re-wrote my Regular Expression to be (?:[^'\\[])test, it just behaved exactly the same as if I hadn’t changed anything – replacing the character preceding test. I looked around StackOverflow, but what I discovered just made me more confident that what I was doing should work.

What am I doing wrong that it’s not working as expected? Am I misusing the pattern?

  • 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-15T17:33:04+00:00Added an answer on June 15, 2026 at 5:33 pm

    If you include an expression for the character in your regex, it will be part of what is matched.

    The trick is to use what you match in the replacement String, so you replace that bit by itself.

    try :

    replaceAll("([^'\[])test", "$1window.test"));
    

    the $1 in the replacement String is a back reference to what capturing group 1 matched. In this case that is the character preceding test

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

Sidebar

Related Questions

I am trying to parse through a document using a DOM Parser in java.
I'm trying to parse a webpage using Java with URLConnection. I try to set
I am using Pattern and Matcher classes from Java to parse a link file
I'm parsing an XML file through Java, and am able to parse through Nodes
I'm trying to parse some HTML using XPath in Java. Consider this HTML: <td
I need to parse the java-objects which are passed through xml. I want to
I've been using java to parse numbers, e.g. (. Integer parseInt numberString) Is there
I'm using Java to parse HTML from a random website, let's say it's http://google.com
I'm using java.text.SimpleDateFormat to parse string representations of date/time values inside an XML document.
I'm using Java 6. I want to parse XHTML that I know is well-formed.

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.