I'm looking for the regex to capture the first letters of a string that might be an email address. If it is an email address, just the first letter of words before the @. In other words, the first letter of words that may or may not be followed by a @, and if there is an @ present, ignore the text after it. For example (captured letters in bold and explanation given on the first 3):
The regex I have so far is /\b[a-z](?=.*@)/g
but it only works if there is an @ present.
For background, I'm trying to capitalize the first letter of names used in an email address. Anything after the @ should be left as is. That's why I really just need to capture lowercase letters at the start of a word. I'm using actionscript which uses the same conventions as javascript.
SOLUTION:
Since actionscript doesn't support lookbehind, I ended up using this code to return the string with capitalized results of the regex:
var pattern:RegExp;
if(string.indexOf("@") == -1) {
// no @ in string, so just find first letters of words
pattern = /\b[a-z]/g;
} else {
// @ exists so just find first letters of words before @
pattern = /\b[a-z](?=.*@)/g;
}
// return the string, capitalizing the results of the regex
return string.replace( pattern, function():String { return String(arguments[0]).toUpperCase(); } );
I think the best way for you to do this is to do 2 searches:
The first would be to capture everything before @
character with:
[^@]+
then to search through THAT list to capture all the first letters:
\b[a-z]
I of course don't know how you are actually implementing any of this (posting some real code may help me help you, FYI) but this seems to be the best option.
If you were using ANY other engine, I would suggest this:
(?<!.*@.*)\b[a-z]
which makes use of lookbehinds, but alas, JS does not have lookbehinds.