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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T13:04:42+00:00 2026-05-26T13:04:42+00:00

When I want to replace some parts of a string, should I call replace

  • 0

When I want to replace some parts of a string, should I call replace directly like this?

var r1 = /"\+((:?[\w\.]+)(:?(:?\()(:?.*?)(:?\))|$){0,1})\+"/g;
arg = arg.replace(r1, function(outer, inner){
    return eval(inner);
});

Or test for a match first, and then replace if it’s a hit, like this?

var r1 = /"\+((:?[\w\.]+)(:?(:?\()(:?.*?)(:?\))|$){0,1})\+"/g;
if (r1.test(arg)) {
    arg = arg.replace(r1, function(outer, inner){
        return eval(inner);
    });
}

I guess this boils down to how the string.replace(regex, string) function works. Will it go into my callback even if there is no match, or will it then simply return arg? In that case I assume the calling replace directly is the right way to go to avoid having the regex engine match the string twice?

  • 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-26T13:04:42+00:00Added an answer on May 26, 2026 at 1:04 pm

    You don have to use test. The function in replace only executed when a match occurs.

    • No matches: No function (eval) call
    • 1 match: 1 call
    • 2 matches: 2 calls
    • etc.

    Also, why are you using eval? eval executes the parameter, as if it’s a JavaScript expression. Since you know the input format, it’s likely that you’re able to achieve the same behaviour without eval.

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

Sidebar

Related Questions

HI, I have some string from XML file, and I I want to replace
I want to do some simple string replace in Bash with sed. I am
How to change case of some backreference in String.replace() ? I want to match
I have a template in word (.docx) format and want to replace some placeholders
I have a paragraph of text and i want to replace some words using
I want to use some Python libraries to replace MATLAB. How could I import
I want to replace the last String which is a , with ) .
Given a string like this: <a href=http://blah.com/foo/blah>This is the foo link</a> ... and a
in among lots of html source i have some elements like this <option value=15>Bahrain
I have a string containing a valid Clojure form. I want to replace a

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.