Using regular expressions to enclose a JSON property value within a new string. Upgrading to MongoDB

For my testing purposes, I am attempting to transfer data from a custom JSON database to MongoDB. The data is currently stored in a json file with the following format:

{ "_id": "213124123114",
  "foo":"bar",
  "otherId": "2324242424",
  ...
}

To maintain the relationships within my test data, I aim to use sed to enclose all _id and xxxId values with ObjectId(...)

The modified data would appear as follows:

{ "_id": ObjectId("213124123114"),
  "foo":"bar",
  "otherId": ObjectId("2324242424"),
  ...
}

I intend to then insert this formatted data into MongoDB.

While testing my regular expressions in JavaScript, I encounter an issue with the following assignment:

var y = s/"_id":(\s?"[0-9]+"),/ObjectId($1)/gi

SyntaxError: Unexpected token :

Despite attempting to escape the ':', it does not resolve the problem.

If I remove the capture flag at the beginning, the regex assignment functions correctly:

var y = /"_id":(\s?"[0-9]+"),/
var p = "\"_id\": \"123123123121321212312\",";
y.test(p) === true

However, without capturing the value block, I am unable to wrap it as needed.

Any suggestions on what could be causing this issue?

Answer №1

Give this a shot:

html.replace(/("(?:-id|differentId)": ?)("\d+")/g, '$1ObjectID($2)');

Check out this demo: http://example.com/demo123

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Revamping the purpose of a function found within an HTML <script> tag

index.html: <script id="change"> function methods(){ return 1; } </script> js.js: ... button.addEventListener("click", ()=>{ document.querySelector("#change").innerHTML = ` function ...

Vue should only activate the element that has been clicked on

I'm currently working on my first Vue project and encountering an issue with triggering a child component within a table cell. Whenever I double click a cell, the `updated` event is being triggered in all child components associated with the table cel ...

Leveraging JWT for Retrieving User Information

Exploring JWT and the MERN stack, currently working on authenticating users with JWT. I've successfully set up JWT in local storage but now facing the challenge of making a GET query request to my MongoDB using only the JWT token. Is it necessary to a ...

Automating the click of JavaScript buttons with Selenium

Test page Experimenting with the above Indeed link to test my selenium automation skills. I am attempting to automate the clicking of the 'apply' button using the Firefox webdriver. My code snippet is as follows: from selenium import webdriver ...

Crafting a Knob Cursor with CSS and AngularJS for a Unique User Experience

After experimenting with the jQuery Knob framework, I encountered challenges when trying to incorporate AngularJS dynamic values. As a result, I opted to create my own CSS-based arc/knobs. The knob displays three values: minimum, maximum, and current valu ...

Troubleshooting the crash in MongoDB bsoncxx when parsing a JSON document containing an int64_t value

When working with generating a JSON document in C# using the Bson library function ToJson(), you may encounter issues. The document typically starts as shown below: { "Key" : NumberLong("2053249000001086"), ... If you are parsing this ...

Issue with a variable causing a reference error in Wordpress JavaScript

As someone who is relatively new to Wordpress theming, I've come across a common issue with Jquery $ code and how it can be misunderstood by Wordpress. It's recommended to use jQuery(document).ready(function($) {} instead of just plain $(.) code ...

Ways to update the DOM following modifications to a data attribute

I'm currently working on a small CMS system that handles translations for static pages in multiple languages. The system refreshes and loads translations dynamically, but I've encountered some bugs that are proving difficult to resolve. One issue ...

Retrieve the $ionicConfigProvider within a Controller

In my controller file named ProfileController.js, I am trying to change the text of the back button. After doing some research, I found this code snippet: $ionicConfigProvider.backButton.text('Go Back').icon('ion-chevron-left'); How can ...

What is the process for importing a file that contains special characters in its name?

Is there a way to correctly import a file with special characters in its name using ES6? I am able to: import { tomorrow} from 'react-syntax-highlighter/dist/esm/styles/hljs'; However, I encounter difficulties when attempting to: import { tom ...

Customizing the JavaScript code created by the "Change Variable by..." block in Blockly

I'm currently working on a Blockly Project where I am passing data from the blocks as JavaScript code. One of my goals is to make some adjustments to the output code in order to make it more user-friendly for beginners. While it is possible to modify ...

Saving additional user information within the users' data

I'm considering a method that might be questionable. I'm thinking of storing a user's likes under the profile attribute in my user object. Here's an example of what it would look like: user = { ... profile: { likes: [ { ...

Exploring Meteor's FS Collection: A guide to efficiently iterate and access CSV files

In my Meteor application, I have a File System collection of CSVs declared as shown below: Uploads = new FS.Collection("yourFileCollection", { stores: [new FS.Store.FileSystem("yourFileCollection", {path: "~/meteor_uploads"})] }); I am trying to iterate ...

Using JavaScript and jQuery to fill a div with content from an external file

Feeling extremely frustrated at the moment. I am struggling to make this work and can't figure out why it's not working. My current challenge involves populating a div with content from another HTML file. Below is the code snippet: <script s ...

Struggling to execute an AJAX request in JavaScript

I am a beginner in .Net development and I am trying to make a call to the client's server. When I test the code using POSTMAN, it works fine. However, when I use the same code/headers in JavaScript, I do not get the desired result. Here is the code I ...

Is there a way to perform updates or inserts of Objects into an inner List within MongoDB?

Blog { id:"001" title:"This blog is just a test", content:"...." comments:[{title:"comment1",content:".."},{title:"comment2",content:"..."}] } comments are nested within the blog. Is there a way to specifically retrieve comment1 only? Als ...

What is the best way to obtain the value of a radio button using ajax?

Here is the search button in my PHP file. I am unsure of how to connect the radio button to the JavaScript file. <button id="submit">Search</button> This is the starting point in the JavaScript file var xhr = new XMLHttpRequest(); f ...

Glitch in the scroll glue directive

Currently, I'm attempting to incorporate scroll-glue in order to automatically scroll to the bottom of a message.html page. My approach involved including the directive 'luegg.directives' in the following manner. (function(){ 'use stri ...

Is it true that event.stopPropagation does not function for pseudoelements?

I am facing an issue with event handling in my ul element. The ul has three li children elements, and the ul itself has a useCapture event handler for click. In the click event handler, I successfully stop the event using event.stopPropagation(), and every ...

The Three.js raycaster is already picking up on objects before I even start moving the mouse

I'm experimenting with the Three.js raycaster and have created a grid of planes that are supposed to start off yellow and turn red when you hover over them with the mouse. However, the issue I'm facing is that when I run the script, all the plane ...