Regex to get the words between @ and .com in the mail address

kali
Tera Contributor

Hi All,

Please help in writing a regex value to get the words between @ and .com in the mail address . 

var email = "user@example.com";

var match = email.match(/(?<=@)[^\.]+(?=\.com)/);

if (match) {

gs.info("Matched word: " + match[0]);

}

After running the code i should get the value example . Thanks in advance

1 REPLY 1

Sandeep Rajput
Tera Patron
Tera Patron

@kali Unfortunately, the Look behind (?<) is not supported in Regex implementation of Javascript. Please refer to this thread https://www.servicenow.com/community/developer-forum/regexp-lookbehind-invalid-quantifier-quot-quot-... to know more about it.

 

Your best bet would to use split as follows.

var email = "user@example.com";

var match = email.split('@')[1].split('.')[0];

if (match) {

gs.info("Matched word: " + match);

}

Hope this helps.