Google Apps Script Regular Expressions
RegEx
A regular expression, also called a regex, is a method for matching text with patterns. For example, a regular expression can describe the pattern of email addresses, URLs, telephone numbers, employee identification numbers, social security numbers, or credit card numbers.What Is a Regular Expression?
A sequence of symbols and characters expressing a string or pattern to be searched for within a longer piece of text.When you search for data in a text, you can use this search pattern to describe what you are searching for. A regular expression can be a single character or a more complicated pattern.
Regular expressions can be used to perform all types of text search and text replace operations.
Ref Google: For additional instructions and guidelines, see also Guidelines for using regular expressions and RE2 Syntax.
Syntax
/pattern/modifiers;
var patt = /googleAppsScript/i;
/googleAppsScript/i is a regular expression.
googleAppsScript is a pattern (to be used in a search).
Regular Expression Modifiers
The Modifiers can be used to perform case-insensitive more global searches:
Modifier Description
i Perform case-insensitive matching
g Perform a global match (find all matches rather than stopping after the first match)
m Perform multiline matching
Using String Methods
In Google Apps Script regular expressions are often used with the two string methods: search() and replace().
The replace() method returns a modified string where the pattern is replaced.
The search() method uses an expression to search for a match and returns the position of the match.
Example
Use String replace() With a Regular Expression
function RegEx1() {try{
var inputString = "This is string example....wow!!! this is really string.";
var outputString = inputString.replace(/ is /g, ' was ');
Logger.log(outputString);
}catch(ex){
Logger.log(ex);
}
}
// O/p: [18-04-01 11:47:31:972 IST] This was string example....wow!!! this was really string
Use String search() With a Regular Expression
function RegEx2() {
try{
var inputString = "Visit My Blog scriptingwithshiva";
var outputString = inputString.search(/scriptingwithshiva/i);
Logger.log(outputString);
}catch(ex){
Logger.log(ex);
}
}
Log: [18-04-01 11:34:50:807 IST] 14.0
Validate email address using RegEx
function RegEx3() {
try{
var emailStr = "abc@gmail.com"
var re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
var isMail=re.test(emailStr);
if(isMail==true){
Logger.log("It is valid email ID")
}else{
Logger.log("It is not valid email ID")
}
}catch(ex){
Logger.log(ex);
}
}
// O/p [18-04-01 12:02:37:295 IST] It is valid email ID
try{
var emailStr = "abc@gmail.com"
var re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
var isMail=re.test(emailStr);
if(isMail==true){
Logger.log("It is valid email ID")
}else{
Logger.log("It is not valid email ID")
}
}catch(ex){
Logger.log(ex);
}
}
// O/p [18-04-01 12:02:37:295 IST] It is valid email ID
Happy Coding...
Thank you! would you know where I can find a reference to all expresions?
ReplyDeleteExtensive regex docs: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions
DeletePractice: https://regexr.com/
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions
Delete