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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T12:25:39+00:00 2026-05-25T12:25:39+00:00

I would like to use create a object that contains regular expressions as the

  • 0

I would like to use create a object that contains regular expressions as the key value. I tried to use the following syntax:

var kv = {
    /key/g : "value"
};

But it fails according JavaScript lint:

SyntaxError: invalid property id

How can I fix it?

Update

Background: The reason why I want to do this is to implement a workaround that fixes wrong unicode in a HTTP API result. I know this is very hackish, but since I have no control over the API server code I think this is the best I can do.

Currently I implemented the mapping by having a keys array and a values array:

function fixUnicode(text) {

    var result = text;
    var keys = [];
    var values = [];
    keys.push(/é/g); values.push("é");
    keys.push(/è/g); values.push("è");
    keys.push(/ê/g); values.push("ê");
    keys.push(/ë/g); values.push("ë");
    keys.push(/à/g); values.push("à");
    keys.push(/ä/g); values.push("ä");
    keys.push(/â/g); values.push("â");
    keys.push(/ù/g); values.push("ù");
    keys.push(/û/g); values.push("û");
    keys.push(/ü/g); values.push("ü");
    keys.push(/ô/g); values.push("ô");
    keys.push(/ö/g); values.push("ö");
    keys.push(/î/g); values.push("î");
    keys.push(/ï/g); values.push("ï");
    keys.push(/ç/g); values.push("ç");

    for (var i = 0; i < keys.length; ++i) {
        result = result.replace(keys[i], values[i]);
    }
    return result;
}

But I want to implement to use a JavaScript object to map keys as values:

function fixUnicode(text) {

    var result = text;
    var keys = {
        /&Atilde;&copy;/g : "&eacute;",
        /&Atilde;&uml;/g :  "&egrave;"
        // etc...
    };

    for (var key in keys) {
        result = result.replace(key, keys[key]);
    }
    return result;
}
  • 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-25T12:25:40+00:00Added an answer on May 25, 2026 at 12:25 pm

    This can be done, but not using object literal syntax. You’ll need to do it like this:

    var kv = {};
    kv[/key/g] = "value";
    console.log(kv[/key/g]); // "value"
    

    Edit: this could probably use some explaining. As xanatos commented below, what’s really happening here is that the key, /key/g in this case, is being toString()‘d to create the key. This is important to know, because it has an effect on key uniqueness. Consider the following:

    var x = {},
        reg = /foo/;
    
    x[reg] = 'bar';
    console.log(x[reg]); // "bar"
    console.log(x[reg.toString()]); // "bar"
    console.log(x['/foo/']); // "bar'
    

    In summary, I’m semi-scared to ask why you need to do this, but assuming you have your reasons, be careful and make sure you understand what is really happening 🙂


    Edit 2: So in response to your updated question, you should be able to achieve something pretty close to what you want. You can use the object literal syntax as long as you wrap the regular expression in quotes. Unfortunately that means you’ll have to manually reconstruct an actually RegExp object out of that key though. For example:

    var result = "abcdef",
        replacements = {
            "/a/g": "FOO",
            "/d/i": "BAR"
        };
    
    for (var key in replacements) {
        var parts = key.split('/');
        result = result.replace(new RegExp(parts[1], parts[2]), replacements[key]);
    }
    
    console.log(result); //FOObcBARef
    

    Edit 3: Because, why not. I was so stuck on making your object-literal syntax work, that I didn’t consider the fact that you never need to actually look up the replacement by the pattern itself (i.e., there’s no need for object keys at all). Here’s a more efficient approach using arrays that doesn’t require the RegExp reconstruction:

    var result = "abcdef",
        replacements = [
            [/a/g, "FOO"],
            [/d/i, "BAR"]
        ];
    
    for (var i = 0, len = replacements.length; i < len; i++) {
        var replacement = replacements[i];
        result = result.replace(replacement[0], replacement[1]);
    }
    
    console.log(result); //FOObcBARef
    

    Edit 4: Because I’m bored and I like this question. Here’s the ninja version:

    var result = "abcdef",
        replacements = [
            [/a/g, "FOO"],
            [/d/i, "BAR"]
        ], r;
    
    while ((r = replacements.shift()) && (result = String.prototype.replace.apply(result, r))) {}
    
    console.log(result); //FOObcBARef
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to create/use a system-wide independent universal 'counter object' that can be
I would like to create one EMF object and use it in various Resource
I would like to use create a rails route for a user's open id.
We would like to use a cronjob to create a database backup. The backup
I would like to create a custom document library where I use the standard
I would like to create an XML-based website. I want to use XML files
I'm trying to use the header() function to create a redirect. I would like
I have a static class which contains a RoutedUICommand that I would like to
I am trying to use Linq to create an object which itself contains 2
I've created some custom UITableViewCells in a nib file and would like to use

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.