views:

39

answers:

2

I need to do a lot of regex things in javascript but am having some issues with the syntax and I can't seem to find a definitive resource on this.. for some reason when I do:

var tesst = "afskfsd33j"
var test = tesst.match(/a(.*)j/);
alert (test)

it shows

"afskfsd33j, fskfsd33"

I'm not sure why its giving this output of original and the matched string, I am wondering how I can get it to just give the match (essentially extracting the part I want from the original string)

Thanks for any advice

+3  A: 

match returns an array. The default string representation of an array in JavaScript is to display the elements of the array separated by commas. In this case the desired result is in the second element of the array:

var tesst = "afskfsd33j"
var test = tesst.match(/a(.*)j/);
alert (test[1]);
Jacob Relkin
ohh ok, I guess I missed that.. thanks, i get confused with javascript sometimes since I'm used to the more restricted way of printing arrays in php
Rick
+1  A: 

I think your problem is that the match method is returning an array. The 0th item in the array is the original string, the 1st thru nth items correspond to the 1st through nth matched parenthesised items. Your "alert()" call is showing the entire array.

BillP3rd