Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
Below is the line of code from a file, and I want to replace the currentVersion value with something else.
I have parsed the file and converted to string. only need a way to replace version.
private currentVersion = "1.26.1.3";
Expected output
private currentVersion = "<different version value>";
Note:
Above line occurs once in file.
version value could be anything
How can it be done with regex and replace in Javascript?
You could replace by captured group
const code = `
import React from "react";
export default App() {
private currentVersion = "1.2.3.4";
}
`
const replaced = "5.6.7.8"
const res = code.replace(
/(private currentVersion = ")(.*)(")/gm,
`$1${replaced}$3`
)
console.log(res)
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 days ago.
Improve this question
I have been searching for the correct regex for converting this string to JSON. It works if there is a single object in the array, but I believe the comma is messing it up somehow.
const json = "{\"userId\":44, \"userName\": \"Jim Coleman\"},{\"userId\":33515, \"userName\": \"Grace Mamaradlo\"}";
const obj = JSON.parse(json.replace(/("[^"]*"\s*:\s*)(\d{17,})/g, '$1"$2"'));
console.log(obj);
There was nothing wrong except that the list of objects was not contained within square brackets
const json = "{\"userId\":44, \"userName\": \"Jim Coleman\"},{\"userId\":33515, \"userName\": \"Grace Mamaradlo\"}";
console.log(JSON.parse(`[${json}]`))
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 months ago.
Improve this question
I just copy and pasted this code from the Internet and it is working, but how do I change the font and text position? I'm working with HTML and I don't know any Javascript.
the script in html:
<script>
const ResultsList = document.getElementById('results')
new URLSearchParams(window.location.search).forEach((value, name) => {
ResultsList.append(`${name}: ${value}`)
ResultsList.append(document.createElement('br'))
})
</script>
Everywhere I do .style.fontWeight="bold"; seems to break the code.
For example, if I do:
<script>
const ResultsList = document.getElementById('results').style.fontWeight="bold";
new URLSearchParams(window.location.search).forEach((value, name) => {
ResultsList.append(`${name}: ${value}`)
ResultsList.append(document.createElement('br'))
})
</script>
The result will simply not even show up on the webpage.
The value of an assignment expression is the value that was assigned. So
const ResultsList = document.getElementById('results').style.fontWeight="bold";
sets ResultsList to "bold". Then you'll get an error when you try to use ResultsList.append(...), because strings don't have an append() method, and you're not appending to the element.
You need to split this into two statements.
const ResultsList = document.getElementById('results');
ResultsList.style.fontWeight = "bold";
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed last year.
Improve this question
I have string data aa = {"PC-lab-network-452":[{"version":4,"addr":"10.186.32.137","OS-EXT-IPS:type":"fixed","OS-EXT-IPS-MAC:mac_addr":"fa:16:3e:39:38:ac"}]}
in javaScript and I've to extract the exact IP address --10.186.32.137 from this data
I'm trying this command--
b = aa.match(\10.186.32.137\g) but it also matches the pattern like 10.186.32.13. I need to match the exact pattern. Any help to fix this?
One of the most simple regex pattern would be:
const aa = `{"PC-lab-network-452":[{"version":4,"addr":"10.186.32.137","OS-EXT-IPS:type":"fixed","OS-EXT-IPS-MAC:mac_addr":"fa:16:3e:39:38:ac"}]}`
const reg = new RegExp(/..\....\...\..../g)
const res = aa.match(reg)
console.log(res[0]);
but as posted in comments why not use JSON.parse?
Parse the string with JSON.parse(STRING) then access your network object ("PC-lab-network-452") JSON.parse(aa)["PC-lab-network-452"] then access any valid array index JSON.parse(aa)["PC-lab-network-452"][0] then access the addr property JSON.parse(aa)["PC-lab-network-452"][0].addr
If you want to solve this without regex. Try this :
const a = {"PC-lab-network-452":[{"version":4,"addr":"10.186.32.137","OS-EXT-IPS:type":"fixed","OS-EXT-IPS-MAC:mac_addr":"fa:16:3e:39:38:ac"}]};
Object.keys(a).forEach(item => {
const ipExist = a[item].find(obj => obj.addr === "10.186.32.137");
if (ipExist) {
console.log(ipExist.addr);
}
else {
console.log('IP not found');
}
});
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I am stuck with this problem. I have a function to accept the path and also the same time I have a variable that I want to condition with.
Here is my problem: I want to make a string type that will act as an access to my variable.
In my situation, I have a roles.operation variable which I want to access it dynamically.
The roles variable has an array with the values of:
roles.operations = ['document','article','document-type'];
with this variable I want this to be access dynamically.
Here is what I've tried, which in replacePath i have the value of document-type:
export const createVariable = (roles,path) => {
const replacePath = path.replace(/-/g,"_");
const finalPath = window[`roles.operations.${replacePath}`];
console.log(finalPath);
}
this gives me undefined.
Try this way:
const finalPath = window['roles']['operations'][replacePath];
or
const finalPath = window.roles.operations[replacePath];
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I have an url that looks like this:
https://example.com/?category=123&dk=sports&dk=groupcompanyinsider&dk=local&lang=en
Is it possible to return every dk parameter separately? (no matter if there will be 1 or 5 dk parameters) so i would get separately sports, groupcompanyinsider, local.
If its not possible maybe there is a way to return all of them in one string like dk=sports&dk=groupcompanyinsiderlocal&dk=local ?
You can use the built-in javascript class URLSearchParams for this.
You can then transform this into the string you want with string concatenation and a foreach.
const url = "https://example.com/?category=123&dk=sports&dk=groupcompanyinsider&dk=local&lang=en";
var params = new URLSearchParams(url);
var result = "";
// concatenate individual values of the 'dk' query parameter
params.getAll('dk').forEach(function (item) {
result += '&dk=' + item;
});
result = result.substr(1); // remove starting '&' from the result;
console.log(result);
The result should contain your desired string.