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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T21:26:41+00:00 2026-05-26T21:26:41+00:00

I have a question about the way .replace() works I know that we can

  • 0

I have a question about the way .replace() works

I know that we can use subpatterns within the replace text like this

var somestr="foobar";
var newstr = somestr.replace(/foo([a-zA-Z]*)/g,"bar$1")
alert(newstr) //"barbar"

However, I am wondering if it is even possible to use the subpattern in an object property, like this.

var somestr = "foobar";
var someobj = {bar:'fuu',two:'uuf'}

var newstr = somestr.replace(/foo([a-zA-Z]*)/g, someobj["$1"])

alert(newstr) //"fuu" ?

My expected result would have been to get the 'bar' property of someobj, which would be "fuu", however the actual result is undefined, which would lead me to believe this is impossible.

How could I otherwise accomplish similar functionality?
Perhaps one of you JS gurus could shed some light on this.

The real-world purpose behind this is I have a template system that uses shorttags to display pieces of data from a requested JSON object, who’s code I am attempting to streamline.

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

    Not like this, I added $1 to show the fail:

    var somestr = "foobar";
    var someobj = {bar:'fuu',two:'uuf', $1: 'test'}
    
    var newstr = somestr.replace(/foo([a-zA-Z]*)/g, someobj["$1"])
    
    newstr; // "test";
    

    but this would work:

    var somestr = "foobar";
    var someobj = {bar:'fuu',two:'uuf', $1: 'test'}
    
    var newstr = somestr.replace(/foo([a-zA-Z]*)/g, "$1")
    
    someobj[newstr]; // fuu
    

    or maybe better yet, replace takes a function:

    var somestr = "foobar";
    var someobj = {bar:'fuu',two:'uuf', $1: 'test'}
    
    var newstr = somestr.replace(/foo([a-zA-Z]*)/g, function () {
        return someobj[arguments[1]];
    })
    
    newstr; // fuu
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a general question about the way that database indexing works, particularly in
I have a question about whether or not a specific way of applying of
I have a question about the proper, best way to manage the model. I
I have a question about locking. This doesn't have to be only about record
(This question is about the best way to temporarily and programatically keep new records
Say I have a string that looks like this: str = The &yquick &cbrown
This is a question about the use of type classes in Haskell. I get
Example: I have some text, like this php code if(empty($condition)) { // work here...
I have question about NSView: Imagine a Custom View where the mouseDown, mouseDrag and
I have question about normalization. Suppose I have an applications dealing with songs. First

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.