简体   繁体   中英

Make 'First.Last@example.com' into 'flast' with JavaScript Regex

I'm not an expert in regexes and was curious if there was a regex to turn this:

First.Last@example.com

to

flast

And more importantly, how it actually works. Currently I am doing this and it does not seem ideal. Thanks!

var email = 'First.Last@example.com';

email = email.substring(0, email.indexOf("@"));
email = email.split('.');
email = _.lowerCase(email[0].charAt(0) +   email[1]);

As per your wishes, this will work

(.)[^.]*\.(.*)@.*

Regex Demo

JS Demo

 var re = /(.)[^.]*\\.(.*)@.*/; var str = 'First.Last@example.com'; var subst = '$1$2'; var result = str.replace(re, subst); document.writeln(result.toLowerCase() + '<br>') 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM