Extracting Characters or Words Between 2 Symbols
Is there any way to extract text between 2 symbols? I have been told to use:
var email = "forename.surname@arco.co.uk";
document.write(email.substring(.,@));
Using substring seems to only work with t开发者_如何学Gohe position of the character and not symbols. I just want to extract the characters between the "." and "@"
Sure, you can use regex:
var lastname = email.match(/[.]([^.]+)@/)[1]
Explanation:
[.] # match dot literally
( # open capture group
[^.]+ # match anything other than a dot
) # close capture group
@ # match @ character
You could use RegExp:
var email = "forename.surname@arco.co.uk";
email.replace(/.+\.(.+)@.+/, '$1'); // surname
Or you could use substring:
email.substring(email.indexOf('.')+1, email.indexOf('@'))
精彩评论