Reputation: 923
I need to remove and modify (email) usernames
examples:
1) [email protected]
2) [email protected]
3) [email protected]
should be result to:
1) max
2) zulubrain
3) topmaster
i have to remove all after @ character and clean special characaters like ".", "-", "#"
what is the best way?
a static example:
var username = "[email protected]";
username.replace(/[^a-zA-Z 0-9]+/g,'');
should clean the name but how can i remove all after "@" ?
Upvotes: 0
Views: 2522
Reputation: 6542
replace(/[^a-z0-9\s]/gi, '')
will filter the string down to just alphanumeric values and
replace(/[_\s]/g, '-')
will replace underscores and spaces with hyphens or put '' as it as per your requirement.
For your requirement :
string.split("@")[0].replace(/[^a-z0-9\s]/gi, '')
Upvotes: 0
Reputation: 133453
You can simply use .split() to extract name then do the cleanup operation using existing code.
An example
var username = "[email protected]".split('@')[0].replace(/[^a-zA-Z 0-9]+/g,'');
Upvotes: 0
Reputation: 59292
You can do this:
var username = "[email protected]";
username = username.split('@')[0].replace(/[\W_]/g,"");
By splitting the code:
username.split('@')[0] // will give all characters before @
.replace(/[\W_]/g,"") // will remove any special character.
Upvotes: 1
Reputation: 15403
Use .split()
in jquery
var username = "[email protected]";
console.log(username.split("@")[0]);
Upvotes: 0