Thursday 15 September 2011

regex - JavaScript match before and after underscore -


I have an array in JavaScript, following the items of the array:

var people = new Array ("michael_51", "mark_57", "graham_44", "paul_22");

I really would like to divide the elements before and after the underscores and then use them in a loop.

  Start loop ... (first match) - (second match) End loop ...   

Loop match will be first (Michael) and The second will be (51) etc.

Many thanks.

Regex is not really necessary (different from split ):
  var people = new array ( "" Post-text "itemprop =" text "> 

Michael_51", "mark_57", "graham_44", "paul_22"); For (var i = 0; i & lt; people.length; i ++) {var data = people [i] .split ("_"); Warning ("(first match):" + data [0] + "- (second match):" + data [1]); }

.

No comments:

Post a Comment