Regular expression for capturing variables

I am trying to use HTML form and javascript (I mention this because some additional regex processing functionality is not available when used in javascript) to accomplish the following:

pass the form to some text and use a regular expression to examine it and "capture" certain parts of it to be used as variables ...

i.e. text:

"abcde email: asdf@gfds.com email: fake@mail.net sdfsdaf ..."

... now, my problem is that I can't think of an elegant way to capture both emails as variables e1 and e2, for example.

the regex I still have is something like this: / email: (\ b \ w + \ b) / g, but for some reason this doesn't return 2 matches ... it only returns asdf @gfds. com> <

sugestions?

0


a source to share


1 answer


You can use RegExp.exec () to reapply a regular expression to a string, returning a new match each time:

var entry = "[...]"; //Whatever your data entry is
var regex = /email: (\b\w+\b)/g
var emails = []
while ((match = regex.exec(entry))) {
    emails[emails.length] = match[1];
}

      



I have saved all emails in an array (to make this work far away with arbitrary input). It looks like your regex might be off a bit too; you will need to change it if you just want to record the complete email.

+3


a source







All Articles