开发者

Extracting Characters or Words Between 2 Symbols

开发者 https://www.devze.com 2023-04-10 21:27 出处:网络
Is there any way to extract text between 2 symbols? I have been told to use: var email = \"forename.surname@arco.co.uk\";

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('@'))
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号