0

Hello,

I am trying to extract ALL strings between two apostraphes within a given string but for some reason I only seem to be getting the first one.

The string I am trying to extract from is:
'1962' '1963' '1964' '1965' '1966'

The code I'm trying to use to get all the extracts is:
var testRE; testRE = results.match("'(.*?)'");

(When I alert the length of testRE it only says 2).

Any help is appreciated, thanks!

3
Contributors
3
Replies
20
Views
3 Years
Discussion Span
Last Post by Troy III
1

Try this

var results ="'1962' '1963' '1964' '1965' '1966'";
var testRE = results.match(/'(.*?)'/g);
alert(testRE.length)
for(var i=0;i<testRE.length;i++){
alert(testRE[i])
}

or this one

    var results = "'1962' '1963' '1964' '1965' '1966'";
    alert(results)
    var testRE; 
    //testRE = results.match("'(.*?)'");
    var testRE = results.replace(/'/g,"").split(" ");

    alert(testRE.length);
    for(var i=0;i<testRE.length;i++){
        alert(testRE[i]);
    }

Edited by Mallika_P: Adding codes

0

An implicit extraction instead of matching the data of interest would be

var results = "'1962' '1963' '1964' '1965' '1966'";
    results.match(/\w+/g);

>>[object Array]["1962", "1963", "1964", "1965", "1966"]
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.