Steve
Steve

Reputation: 4898

Pulling value from string with RegEx

I have the string

text ='something here width="560" something here'

and I would like to pull out the width value. I tried

width = text.match(/width=\"[^\"]/g);

but that just returns an array with one value = 5. Could someone give me a nudge in the right direction?

Thanks

Upvotes: 0

Views: 46

Answers (2)

zerkms
zerkms

Reputation: 254926

/width=\"[^\"]+/g
              ^--- + means 1 or more

Without quantifier it matches exactly one character.

Upvotes: 1

Ry-
Ry-

Reputation: 224921

Looks like you want:

var m = text.match(/width="([^"]+)/);
var width = m && m[1];

Note the + — a character class will only match one character by default.

Upvotes: 3

Related Questions