notAChance
notAChance

Reputation: 1430

Take numerous parts of string out of long string

I have a fairly long String

var str = "[FirmLegalEntity [firmLegalEntityCode=F2, name=Global Markets Limited, shortName=CMP, firmLeCodeShortName=null], FirmLegalEntity [firmLegalEntityCode=D7, name=BHG SINGAPORE LIMITED, shortName=CGL, firmLeCodeShortName=null]]"

I need all firmLegalEntityCodes.

I've tried using Str.shift("=,") but this gets nowhere near the desired output.

There is also the issue that other values in the string are surrounded by = and ,.

Upvotes: 0

Views: 45

Answers (2)

mplungjan
mplungjan

Reputation: 177786

My version

var str = "[FirmLegalEntity [firmLegalEntityCode=F2, name=Global Markets Limited, shortName=CMP, firmLeCodeShortName=null], FirmLegalEntity [firmLegalEntityCode=D7, name=BHG SINGAPORE LIMITED, shortName=CGL, firmLeCodeShortName=null]]";

var leg = str.match(/firmLegalEntityCode\=.{2}/g).map(function(str) {
  return str.split("=")[1]
});
console.log(leg)

Upvotes: 0

fafl
fafl

Reputation: 7385

Try a regex:

var re = /firmLegalEntityCode=([^,]*),/g;
var s = '[FirmLegalEntity [firmLegalEntityCode=F2, name=Global Markets Limited, shortName=CMP, firmLeCodeShortName=null], FirmLegalEntity [firmLegalEntityCode=D7, name=BHG SINGAPORE LIMITED, shortName=CGL, firmLeCodeShortName=null]]';
var m;
while (m = re.exec(s)) {
    console.log(m[1]);
}

Upvotes: 2

Related Questions