Shashank amin
Shashank amin

Reputation: 151

Javascript match

Suppose this is my code

var str="abc=1234587;abc=19855284;abc=1234587;abc=19855284;abc=1234587;abc=19855284;abc=1234587;abc=19855284;abc=1234587;abc=19855284;abc=1234587;abc=19855284;abc=1234587;abc=19855284;abc=1234587;abc=19855284;abc=1234587;abc=19855284;abc=1234587;abc=19855284;"; 

var patt1=/abc=([\d]+)/g;
document.write(str.match(patt1));

i want the output as 1234587,19855284

this doesnt return the number but instead returns the complete string which is in the pattern if i remove 'g' from the pattern it returns abcd=1234578,1234578 what am i doing wrong??

Upvotes: 1

Views: 547

Answers (3)

bhappy
bhappy

Reputation: 62

If this is what you want

1234587,19855284,1234587,19855284,1234587,19855284,1234587,19855284,1234587,19855284,1234587,19855284,1234587,19855284,1234587,19855284,1234587,19855284,1234587,19855284

then try this

var str="abc=1234587;abc=19855284;abc=1234587;abc=19855284;abc=1234587;abc=19855284;abc=1234587;abc=19855284;abc=1234587;abc=19855284;abc=1234587;abc=19855284;abc=1234587;abc=19855284;abc=1234587;abc=19855284;abc=1234587;abc=19855284;abc=1234587;abc=19855284;";

var patt1=/([\d]+)/g;
document.write(str.match(patt1));

or you can use the array index as sjngm mentioned

Upvotes: 0

Amit Soni
Amit Soni

Reputation: 3316

Try following code.

var str = "abc=1234587;abc=19855284;abc=1234587;abc=19855284;abc=1234587;abc=19855284;abc=1234587;abc=19855284;abc=1234587;abc=19855284;abc=1234587;abc=19855284;abc=1234587;abc=19855284;abc=1234587;abc=19855284;abc=1234587;abc=19855284;abc=1234587;abc=19855284;";
        str = str.replace(/abc=/gi, '');
        document.write(str);

Upvotes: 1

sjngm
sjngm

Reputation: 12871

match() returns an array. The first entry (index 0) is always the matching string. Following that you get the matching group(s).

The toString()-logic of an array takes all elements and joins them with ", ". You can use e.g. join("-") to change that.

Upvotes: 2

Related Questions