Multiple Javascript Regex Group

How can I get all regex groups?

var str = "adam peter james sylvester sarah"; var regex = /what should my regex be t capture all names that has the letter a in them/ var match = regex.exec( text ); console.log(match) 

What I want here is every name that has the letter a in it ... I want to be able to capture several names, preferably at the same time.

Is it possible?

0
source share
2 answers

Try my Rubular example

 var str = "adam peter james sylvester sarah"; var match = str.match(/[az]*a[az]*/gi) console.log(match) 
+1
source

The regex is probably too large for this situation. I think it would be easier .split() and .indexOf() to do so

 var names = str.split(" "); for ( var i=0; i < names.length; i++ ) { if ( names[i].indexOf("a") >= 0 ) console.log(names[i]); } 
0
source

All Articles