javascript regular expression to grab the state of 2 characters that is always following a city and a comma

Seems that google api for returns city and state from a zip code is just poorly made as they return a json object with address_components of which all have array list of elements with long_name and short_name

Now that would be fine , except that i cannot count on the state because in 3rd or the 4th array. :/

The ONE thing that i do see in common is a formatted_address of which example is

formatted_address: "Lake Villa, IL 60046, USA"

Since that is the case then seems that a regex is what i want


var city = "Lake Villa"
var state = "IL" 

I don’t want to replace…. here is some regex i was trying to work with

 var formatted_address = "Lake Villa, IL 60046, USA";
 alert(formatted_address.replace(/(.+)-([A-Z]{2})$/, "$1, $2").replace("-", " "));

Source: stackoverflow-javascript

How do I a get SPECIFIC string from webpage and then use it in my own JavaScript code?

So basically, I need to get a SPECIFIC number from a website’s page source via Google Chrome and then use that number in my own js code. The number from the website is not from it’s HTML, but from one of it’s scripts. There are some pictures below to help you better understand.

View image here, because I don’t have enough ‘reputation points’…

As you can see in the picutre circled in red, that is the number I want to take. It changes everytime the page reloads so I need a script (if possible) that will reload the page, find and take that number (it will change everytime it reloads), and then use it in my own JavaScript function as a variable or something.

The url in Google Chrome is: “view-source:

NOTE: Because this is from a page where the user has to be signed in, you will not get the same page source code but that’s why I gave you a picture.

I have searched everywhere on the internet and cannot find any answer to my question so maybe you could help. Thanks a lot!

Source: stackoverflow-javascript

How do I call for a certain variable or another depending on a string I have

I have two variables, varL and varR, which have the information that they are L and R respectively. Depending on wether a third object has as imput ‘L’ or ‘R’, I would like to reference varL or varR respectively. Is there any way to do this?

Specifically, varL and varR are arrays. If the third object has as imput ‘L’, I would like to have access to varL[i] for some i, and the same goes for ‘R’. I know this is not even remotely correct, but I was looking for something like

(var +’L’)[i] or something

Source: stackoverflow-javascript

String to Object with JavaScript

I would like to know the most efficient way to convert this string into an object in JavaScript.

This is the ugly string:


At the moment I’m using this function, but it does not work fine, the expected output would be a object of object with the keys date, initRegistrations, registrations, finalizeRegistrations:

   function convertToArrayOfObjects(data) {
    var keys = data.shift(),
        i = 0, k = 0,
        obj = null,
        output = [];

    for (i = 0; i < data.length; i++) {
        obj = {};

        for (k = 0; k < keys.length; k++) {
            obj[keys[k]] = data[i][k];



    return output;

Is there any fancy way to achieve this easily or I should go playing with my array of objects?
Any help or advice is appreciated.

Source: stackoverflow-javascript

Splitting a Long String Into Multiple Lines using javascript

I have the following string:

"The water content is considered acceptable for this voltage class. Dielectric Breakdown Voltage is unacceptable for transformers > 288 KV. Power factors, Interfacial Tension and Neutralization Number are acceptable for continued use in-service.".

I want to split the string into lines so that every line will contain at max 5 sentences in each line.

I want to control the number of sentences in each line dynamically, so that tomorrow I will be able to split the string into lines where each line contain at max N sentences in each line.

Source: stackoverflow-javascript