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
My goal is to pull chunks of 0's or 1's from a binary string. Using regex.match to look for patterns pulls out all chunks and loses order. I need to be able to pass any length of binary to it. How can I correctly pull out chunks so that '10011000001' -> '1', '00', '11', '00000', '1'?
I can only think to run a loop to count the number of changes from 0 to 1 then run alternating regex.match() on it, but that's certainly inefficient.
You could look for a digit and look for more of the same group.
var string = '10011000001',
parts = string.match(/([01])\1*/g);
console.log(parts);
Related
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 3 years ago.
Improve this question
I need to tokenize a string representing an item in inventory in javascript getting words and numbers, for example:
Given the string 'Plane Engine 50x60' the tokens should be ['Plane','Engine','50','x','60']
Given the string 'Car wheel 220v' the tokens should be ['Car','wheel','220','v']
The solution could be a RegExp or a Javascript Algorithm
All the items have that kind of names come has measures like 20x30 and others have electronic info such as 220v. The thing I need to do is split like the examples above.
Thanks
We can split by groups of either numbers or letters and then filter out the empty strings or spaces.
'Car wheel 220v'.split(/([0-9]+|[a-zA-Z]+)/).filter(token => (token.match(/[a-zA-Z0-9]/)))
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"')));
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
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 9 years ago.
Improve this question
Lets say you needed a hardcoded list of strings (the question here is not whether or not you should hard-code things).
Is there a reason to prefer this:
var things = 'a b see'.split(' ');
over this:
var things = ['a', 'b', 'see'];
Pros of first approach:
Easier to refactor data from code. (String is not language specific)
Pros of second approach:
Less prone to error (what if your string has a space in it?)
Easier to format automatically (can and probably should separate each string by newline)
Easier to get data from alternate sources in the future.
Signals intent more clearly
Tiny bit more efficient