Javascript match
Suppose this is my code
var str="abc=1234587;abc=19855开发者_高级运维284;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??
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.
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);
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
精彩评论