0

I am trying to use an HTML form and javascript (i mention this, because some advanced features of regex processing are not available when using it on javascript) to acomplish the following:

feed the form some text, and use a regex to look into it and "capture" certain parts of it to be used as variables...

i.e. the text is:

"abcde email: [email protected] email: [email protected] sdfsdaf..."

... now, my problem is that I cannot think of an elegant way of capturing both emails as the variables e1 and e2, for example.

the regex I have so far is something like this: /email: (\b\w+\b)/g but for some reason, this is not giving back the 2 matches... it only gives back [email protected] ><

sugestions?

1 Answer 1

3

You can use RegExp.exec() to repeatedly apply a regex 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 stored all the e-mails in an array (so as to make this work far arbitrary input). It looks like your regex might be a little off, too; you'll have to change it if you just want to capture the full e-mail.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. Yeah, my regex might be a little off, since i wanted to capture the addresses by using the parethesis... but your answer really helps

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.