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 3 years ago.
Improve this question
How to convert string to valid json ?
const string = "[{foo:bar,key:value},{foo:bar,key:value}]";
So that i can parse it using JSON.parse(string).
Note that : i cant manually put (") to every keys and values. I am stack here that's why i am on stackoverflow.
Using regex to replace any words or numbers will work for the json you have supplied, it will not however work if you have mixed value and "value" properties.
var text = "[{foo:bar,key:value},{foo:bar,key:value}]";
console.log(JSON.parse(text.replace(/(\w+|\d+)+/g, '"$1"')));
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 14 days ago.
The community is reviewing whether to reopen this question as of 14 days ago.
Improve this question
How do I check if part of string is valid JS syntax? I heard that acorn.parse returns true or false if given string is valid JS syntax or not. However, what if only part of string is valid JS syntax code. How should I aproach this?
let str = "This string contains words and valid JS syntax. I should extract only the part which is the syntax. function foo(){}"
extractValidSyntax(str) // "function foo() {}"
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
Im working on a simple program and im trying to put user input in it and i want to know if theres an easy way of doing that.
You can split on the comma and use map along with the unary plus operator to convert each element to a number.
str.split(",").map(a=>+a);
const str = "1,2,3,4";
const arr = str.split(",").map(a=>+a);
console.log(arr);
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 a simple Regex to match 'der' - /der/g
I need the context around the match. Is there any way to build the regex that way? Thank you in advance.
If you want to match the context plus the search string, simply add the optional range before and after the searched string: .{0,5}der.{0,5}
If you want to get the context only, you have to fetch it separately: (.{0,5})der(.{0,5})
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 7 years ago.
Improve this question
I need to modify a string in JavaScript as required.suppose
I have a string 4x i need to insert * between 4 and x and
get it as 4*x
if it is 9x i should get it as 9*x etc
if it is sin(4x) i should get it as sin(4*x)
if it is sin(x) i should get it as sin(x).
if it is sin(4 x+5) i should get it as sin(4*x+5)
how to do this in java script.
any help is appreciable.
Use replace() with capturing group
document.write('4x , 5x , 5x+3 , sin(4 x+5) , sin(4x)'.replace(/(\d+)\s*([a-z])/ig,'$1*$2'));
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 8 years ago.
Improve this question
I would like to make a caching mechanism. I want to drop the last digit of the latitude and longitude for example :
look below how to make request. important things are the "lat" and "lon" values. There must be four digits after the full stop (also known as a period).
3 Request /v1/x/y/?lat=40.12225&lon=2.13422&radius=1&key=abc
caching look up: /v1/x/y/?lat=40.1222&lon=2.1342&radius=1&key=abc-> no match (look at lat=40.1222&lon=2.1342)
You can use the following regex to trim your strings to the correct format:
/(?:lat|lon)(=\d+\.\d{4})\d*/g then you replace with $1
Example:
var s = "/v1/x/y/?lat=40.12323&lon=2.13421&radius=1&key=abc"
s.replace(/((?:lat|lon)=\d+\.\d{4})\d*/g,"$1");
Output:
/v1/x/y/?lat=40.1232&lon=2.1342&radius=1&key=abc